Java 14 was released for General Public usage on 17 March 2020. It brought quite a number of significant improvements for developers. Among them, Helpful NullPointerExceptions is one of the key improvements released under JEP 358. You can download Java 14 by clicking here.

Java 14 - Helpful NullPointerExceptions...!!! Click To Tweet

Check out: JAVA 13 Features with examples

JEP 358 – Helpful NullPointerExceptions

There is an improvement being made in the message thrown by NullPointerExceptions by JVM. Earlier whenever NullPointer exception used to occur, it throws a generic message but could highlight in the message that which variable threw that Null pointer exception. But, Thanks to Java, they have extended the message by adding -XX:+ShowCodeDetailsInExceptionMessages as a VM argument. Once added, It will tell that which variable is actually null.

Here is an example that will throw Null pointer exception:


Before Java 14

In Java 14 (After adding -XX:+ShowCodeDetailsInExceptionMessages)

Let’s say if you are running in Eclipse, try to add ‘-XX:+ShowCodeDetailsInExceptionMessages’ as a VM argument in the Run Configurations and then execute the below code.

Java 14 - Helpful NullPointerExceptions...!!! Click To Tweet

Other Useful References:

    • What is new in Java 13