Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java streams findAny() encounters null pointer exception after filter() operation filters out everything

I am having trouble figuring why findAny() throws a null pointer exception after filter() operation on a stream. In this particular test case, the filter operation should have filtered out everything, leaving no results for findAny().

Optional<JsonNode> encryption = sseEncryptionList.stream()
                  .filter(n -> n.textValue().equals("AES256")) //Filters out everything
                  .findAny(); //Throws null pointer exception

The stack trace:

Exception in thread "main" java.lang.NullPointerException
at example.Main.lambda$main$0(Main.java:41)
at java.util.stream.ReferencePipeline$2$1.accept(ReferencePipeline.java:174)
at java.util.ArrayList$ArrayListSpliterator.tryAdvance(ArrayList.java:1351)
at java.util.stream.ReferencePipeline.forEachWithCancel(ReferencePipeline.java:126)
at java.util.stream.AbstractPipeline.copyIntoWithCancel(AbstractPipeline.java:498)
at java.util.stream.AbstractPipeline.copyInto(AbstractPipeline.java:485)
at java.util.stream.AbstractPipeline.wrapAndCopyInto(AbstractPipeline.java:471)
at java.util.stream.FindOps$FindOp.evaluateSequential(FindOps.java:152)
at java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.util.stream.ReferencePipeline.findAny(ReferencePipeline.java:469)
at example.Main.main(Main.java:42)

Is this expected behavior for findAny() to throw a null pointer exception if the stream doesn't contain any elements?

Edit: What is an elegant and functional way to resolve filter operations that might filter out all elements?

like image 251
mrQWERTY Avatar asked Nov 02 '17 18:11

mrQWERTY


2 Answers

The best way to avoid NPE is:

Optional<JsonNode> encryption = sseEncryptionList.stream()
              .filter(Objects::nonNull)
              .filter(n -> "AES256".equals(n.textValue()))
              .findAny();

"AES256".equals(n.textValue())) will not throw NPE if n.textValue() is null

like image 144
Yegor Babarykin Avatar answered Sep 22 '22 09:09

Yegor Babarykin


Your confusion comes from the fact that you filter first - and you would expect the filter method to be somewhere in the stack trace; but notice that intermediate operations do nothing per-se, they are executed lazily and only kick in when a terminal operation is present (such as findAny in your case). It's actually there in the stack trace (the filter method), but it looks like this: Example.Main.lambda$main$0(Main.java:41).

If you want to filter null values, first do:

.filter(Objects::nonNull)
.filter(n -> n.textValue().equals("AES256"))
.findAny()
like image 33
Eugene Avatar answered Sep 19 '22 09:09

Eugene