Java 8 stream - how to properly make NPE-safe stream

2018-10-29T15:50:54

Last week got very strange NPE in a stream that caused my lots of troubles so right now I'm feeling like being over-safe with NPE when using a Stream.

Here is my method right now:

private boolean matchSomeError(final List<ErrorAtMessageLevel> errorList) {
    return errorList.stream()
        .filter(errorAtMessageLevel -> errorAtMessageLevel.getErrorSegment() != null && errorAtMessageLevel.getErrorSegment().getErrorDetails() != null)
        .map(errorAtMessageLevel -> errorAtMessageLevel.getErrorSegment().getErrorDetails())
        .anyMatch(errorDetails -> SOME_FANCY_ERROR_CODE.equals(errorDetails.getErrorCode()));
}

My problem is that I'm dealing with external POJO here so I cannot change it and make it null-safe so I have to adjust my code.

Here are some restrictions: 1) errorList - cannot be null here so a call to .stream() is safe - when it's empty it will just return false 2) getErrorSegment() and getErrorDetails() can both be nulls tha's why I'm using filter like that to make sure none of them is null 3) getErrorCode() can be null but it will never throw NPE because it will just return false when matching with null - fine by me.

How would you go about making that stream better? I feel like my .filter() is bad and it could be made better. Writing lots of code like that lately because I'm not sure anymore how the stream is working with nulls and don't want to get NPE in .map() because it's called on null

Copyright License:
Author:「doublemc」,Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.
Link to:https://stackoverflow.com/questions/53040979/java-8-stream-how-to-properly-make-npe-safe-stream

About “Java 8 stream - how to properly make NPE-safe stream” questions

Last week got very strange NPE in a stream that caused my lots of troubles so right now I'm feeling like being over-safe with NPE when using a Stream. Here is my method right now: private boolean
I'm practicing streams in java 8 and im trying to make a Stream&lt;Integer&gt; containing the multiples of 2. There are several tasks in one main class so I won't link the whole block but what i go...
While working with Java 8 Streams, I some times find that Stream doesn't have a particular method that I desire (e.g. takeWhile(), dropWhile(), skipLast()). How do I create my own stream class whi...
I think this is a fairly basic question concerning Java 8 streams, but I have a difficult time thinking of the right search terms. So I am asking it here. I am just getting into Java 8, so bear wit...
I have a java 8 stream with underlying IOStream and I want to make sure that my method closes that stream. Is there any way to check it with unit test?
I am studying Java8 esp Stream API. but still don't get it how stream and map work. what i understood of stream was like the result will be 1111 2222 when i use peek() and forEach() but the result...
I am trying to compute Prime numbers with Java8 streams, but I get into an IllegalStateException : stream has already been operated upon or closed. This is my code : package experimentations.chap...
How can I reuse in java8 (maybe a memoization process) values already computed through iteration over a stream? If the stream is duplicated or supplied again it will be recomputed. In some cases it
If we use Java 8 Stream like list.stream().filter(....).collect(..)..... When is it closed this stream? Is it good practice that we close the stream us as the next example? Stream&lt;String&gt; ...
If we use Java 8 Stream like list.stream().filter(....).collect(..)..... When is it closed this stream? Is it good practice that we close the stream us as the next example? Stream&lt;String&gt; ...

Copyright License:Reproduced under the CC 4.0 BY-SA copyright license with link to original source & disclaimer.