Java 14 – Helpful NullPointerException (NPE)

Java 14 (JEP 358) improves the usability of NullPointerException generated by the JVM by describing precisely which variable was null. Lets understand this in detail.

1. Better NullPointerException

First, we need to pass -XX:+ShowCodeDetailsInExceptionMessages JVM flag to enable this feature while running the application. Make sure, you are passing it.

The option will first have default false so that the message is not printed. It is intended to enable code details in exception messages by default in a later release.

-XX:+ShowCodeDetailsInExceptionMessages

Now run a very simple program to understand how the improved error message looks like. In this program, at line number 7, we declared a variable e which has not been initialized. It could be anything like method argument or some return value.

In line 9, we try to access name attribute using its getter method.

package com.howtodoinjava.core.basic;

public class HelpfulNullPointerException 
{
	public static void main(String[] args) 
	{
		Employee e = null;
		
		System.out.println(e.getName());
	}
}

class Employee {
	Long id;
	String name;
	
	public Long getId() {
		return id;
	}
	public void setId(Long id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
}

Notice how the message now clearly tell which method invocation caused NPE and which varible was null.

Also note that the JVM displays an exception message on the same line as the exception type, which can result in long lines. For readability in a web browser, it shows the null-detail message on a second line, after the exception type.

Exception in thread "main" java.lang.NullPointerException: 
	Cannot invoke "com.howtodoinjava.core.basic.Employee.getName()" because "e" is null
	at com.howtodoinjava.core.basic.HelpfulNullPointerException.main 
	(HelpfulNullPointerException.java:9)

If we do not use this feature, the traditional error message has been like this:

Exception in thread "main" java.lang.NullPointerException
	at com.howtodoinjava.core.basic.HelpfulNullPointerException.main
	(HelpfulNullPointerException.java:9)

2. Technical Details

Only NPEs that are created and thrown directly by the JVM will include the null-detail message (messages we generally pass in the constructor when we create the exception in the program). NPEs that are explicitly created and/or explicitly thrown by programs running on the JVM are not subject to the bytecode analysis.

Note that the null-detail message might not be wanted in all circumstances because of few reasons. For example, it will impact performance because the algorithm adds some overhead to the production of a stack trace.

Also, it add security-risk because the null-detail message gives insight into source code that is otherwise not easy to obtain.

Drop me your question in comments related to this Helpful NullPointerException feature added in Java 14.

Happy Learning !!

Was this post helpful?

Join 7000+ Fellow Programmers

Subscribe to get new post notifications, industry updates, best practices, and much more. Directly into your inbox, for free.

Leave a Comment

HowToDoInJava

A blog about Java and its related technologies, the best practices, algorithms, interview questions, scripting languages, and Python.