You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
import org.checkerframework.checker.nullness.qual.Nullable;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
public class Example {
public void doSomething() {
List<@Nullable Integer> list = Arrays.asList(null, 1, 2);
var comparator =
Comparator.<Integer>nullsLast(Comparator.naturalOrder());
System.out.println(list.stream().max(comparator));
}
public static void main(String[] args) {
Example example = new Example();
example.doSomething();
}
}
Command to run it, nullness checker is fine with it, but end up in NPE.
Exception in thread "main" java.lang.NullPointerException
at java.base/java.util.Objects.requireNonNull(Objects.java:222)
at java.base/java.util.Optional.<init>(Optional.java:107)
at java.base/java.util.Optional.of(Optional.java:120)
at java.base/java.util.stream.ReduceOps$2ReducingSink.get(ReduceOps.java:129)
at java.base/java.util.stream.ReduceOps$2ReducingSink.get(ReduceOps.java:107)
at java.base/java.util.stream.ReduceOps$ReduceOp.evaluateSequential(ReduceOps.java:913)
at java.base/java.util.stream.AbstractPipeline.evaluate(AbstractPipeline.java:234)
at java.base/java.util.stream.ReferencePipeline.reduce(ReferencePipeline.java:558)
at java.base/java.util.stream.ReferencePipeline.max(ReferencePipeline.java:594)
at Example.doSomething(Example.java:11)
at Example.main(Example.java:16)
The text was updated successfully, but these errors were encountered:
The documentation for Stream#min/max say that they NPE if the min/max is null.
It seems okay to pass a comparator that accepts null, but there is no way to say that null should never be the maximum.
It would be nice to forbid nullsLast with max and nullsFirst with min, only on nullable streams. But we would need some separate way to track that and that seems like overkill.
Take this example,
Command to run it, nullness checker is fine with it, but end up in NPE.
The text was updated successfully, but these errors were encountered: