今天在写一个将得到的list集合中某两个属性放入到map集合中,但是一直报空指针异常
我的代码如下:
获取到的是一个对象RecordDictDetail的list集合,首先使用filter进行了过滤,将满足条件的数据筛选出来,然后将对象中的dictKey作为map集合的key,dictValue作为map集合的value,但是却发现collect那块报空指针异常,点进去源码查看原因
Map<String, String> collect = recordDictDetailList.stream().filter(e -> e.getEntNum().equals(entNum)).collect(Collectors.toMap(RecordDictDetail::getDictKey, RecordDictDetail::getDictValue));
源码:Collectors.toMap(key,value)底层调用了merge方法,对于参数进行了约束,这里的value值不允许为空,如果value值为空,他就会报空指针异常。经过查询数据库中数据,发现我的数据中存在dictValue没有赋值的情况,修改数据库中的数据,解决。
default V merge(K key, V value,
BiFunction<? super V, ? super V, ? extends V> remappingFunction) {
Objects.requireNonNull(remappingFunction);
Objects.requireNonNull(value);
V oldValue = get(key);
V newValue = (oldValue == null) ? value :
remappingFunction.apply(oldValue, value);
if(newValue == null) {
remove(key);
} else {
put(key, newValue);
}
return newValue;
}