为什么 Collectors.groupingBy 会抛出 NullPointerException?
Caused by: java.lang.NullPointerException
at java.util.stream.Collectors.lambda$groupingBy$45(Collectors.java:907)
at java.util.stream.ReduceOps$3ReducingSink.accept(ReduceOps.java:169)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1374)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:481)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:708)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.collect(ReferencePipeline.java:499)
at com.evry.integrator.terpcrm.schedular.CustomerTeamsJmsServiceImpl.importDataFromTerpAndNotifyTopic(CustomerTeamsJmsServiceImpl.java:90)
复合键:
//Entity with Getter and setters
public String getCompositeKey() {
return getRegistryId() + "_" + getPersonPartyId() + "_" + getSource()+ "_" + getRoleName();
}
//Earlier following was composite key and it works fine; when added another field...NPE but when i revert to following no issues
//public String getCompositeKey() {
// return getRegistryId() + "_" + getPersonPartyId() + "_" + getSource();
//}
根据之前工作正常的复合键在表中查找重复项的代码,但现在添加另一个字段会引发 NPE:(DB 中不存在空值)
Set<String> duplicates = xxedgeCrtV.stream()
.collect(Collectors.groupingBy(XxedgeCrtV::getCompositeKey, Collectors.counting()))
.entrySet().stream()
.filter(e -> e.getValue() > 1L)
.map(e -> e.getKey())
.collect(Collectors.toSet());
更多数据:
在 40,000 条记录中;有 10 条记录,getRoleName 为空,但是当我通过全部四个分组查询计数时,它不返回任何记录,这意味着所有记录。
在 void main 中,当我尝试以下操作时,没有 NullPointerException:
String a = null;
String b = "gigi";
String c = "migi";
String d = "figi";
System.out.println(d+ "_" + c+ "_" +b + "_"+a);
Output: figi_migi_gigi_null
笔记:
我已经处理了 getRoleName null 案例,但仍然是同样的问题。