-3

The following two lines:

   Boolean visitedAlphabet[] = new Boolean[26];
   Arrays.stream(visitedAlphabet).anyMatch(e -> e != true);

Produce a nullpointer, the second line to be specific:

Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Boolean.booleanValue()" because "<parameter1>" is null

Could you please tell me why this happens?

M. Lee
  • 99
  • 3
  • 7

1 Answers1

5

EDIT - Adding John Bollinger's excellent comment snippet -

Because e is null and the runtime is trying to "unbox" it to a primitive boolean.

You can either do (e != null && e != true) or !Boolean.TRUE.equals(e) or initialize your array:

Initialize an array stuff:

How to initialize an array in Java?

Boxing and unboxing:

https://docs.oracle.com/javase/tutorial/java/data/autoboxing.html

mikeb
  • 10,578
  • 7
  • 62
  • 120