Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Why does this line produce a nullpointer? Java 15

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

EDIT – Adding John Bollinger’s excellent comment snipped –

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading