Why is Collectors.groupingBy throwing 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)
Composite Key:
//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();
//}
Code to find duplicates in table based on composite key which was working fine earlier, but now adding another field throws an NPE: (there are no nulls present in 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());
More data:
Out of 40,000 records; 10 records are there with getRoleName as null, but when I query the count by grouping by all four, then it does not return any record, which means all are.
In void main, when I try following, no 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
Note:
I have handled getRoleName null case, but still same issue.