Java 14 NullPointerException Detailed Message

Published by user on

NullPointerException is something which every developer faces and it is difficult to find where the exception occurred. Java 14 made it really convenient for programmers to find the NullPointerException in the code. It adds a really good description specifying where the exception occurred along with the variable name. Let’s look at the examples.

Before Java 14 the description of NullPointerException looked something like this.

NullPointerException Description Before Java14

Note: Before proceeding please set the following VM argument, as Java 14 only shows message if the –XX:+ShowCodeDetailsInExceptionMessages flag is set.

Set ShowCodeDetailsInExceptionMessages VM Argument

NullPointerException Example With Java14

public class Java14NullPointerException {
	public static void main(String[] args) {
		String name = null;
		String temp = name.toLowerCase();


Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.toLowerCase()" because "name" is null
	at Java14NullPointerException.main(

NullPointerException Description With Java14

Above output clearly shows that the variable ‘name’ is null and toLowerCase() method got invoked on it.

In the message we also got the line number where the NullPointerException occured.

As a best practice keep ShowCodeDetailsInExceptionMessages enabled only on development and QA environments, as it gives more details about your source code. And in some cases we don’t want to log very detailed messages to the log files.

Hope you understood the way NullPointerException works in Java 14. Please share this article if you liked it.


Categories: Java