I have:
List<Optional<MyObject>> myList;
This list is populated by reading in from files. Once the file read is finished, I do a check against empty list:
if(myList.size() == 0){//}
and then proceed as follows:
myList.stream()
.filter(Optional::isPresent)
.map(i → i.orElse(new MyObject(“adventureBook”, 20))
.collect(groupingBy(MyObject::getBookType, TreeMap::new, mapping(MyObject::getBookPrice, toList())));
I have around 350k MyObject files to read in from, 300k files are read in fine but when I try to read in the entire batch of c.350k files it throws a null pointer exception on the collect().
How is it possible that despite wrapping in Optional<> and checking Optional::isPresent, Optional::orElse etc still a null object manages to sneak through and given that I have such a large number of files what is the best way to try and narrow down on the errant file(s)? Thanks
edit: stack trace added
Exception in thread "main" java.lang.NullPointerException
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
at java.util.ArrayList$ArrayListSpliterator.forEachRemaining(ArrayList.java:1382)
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.mypackage.MyObject.main(MyObject.java:108)