The JDK-14 is released production ready on 17th March 2020. JDK 14 comes with multiple new features and enhancements.

In this article we will discuss Helpful NullPointerException feature.

This feature of JDK 14 makes the NullPointerException more informative, thus help the programmer understand the actual cause of premature end of the program.

Lets go into the details…

We know that NullPointerException can occur at any join point of the Java program. And it is difficult to have null check or explicitly handle this exception at every point. In most occasions we depend on JVM to raise this error. This is one of the reasons to define NullPointerException as unchecked exception.

For example, when we run the below code, JVM creates and throws NullPointerException. This exception comes with an information, qualified method name, filename and the line number of the source. The Figure-1 shows error message running below code.

package com.techxposer.sample;

public class EmployeeService {
 
 
 public void processEmployee(Employee employee){
 
 employee.getFirstName();
 }
 
 public static void main(String s[]){
 
 new EmployeeService().processEmployee(null);
 }
}

Figure-1: Exception message

This describes a NullPointerException occurred at line number 8 in EmployeeService class. And from this information developer can identify the source of this exception is employee.getFirstName() thus employee is a null reference.

This is a simple expression, but we have much complex statements in regular programs. Most the time we use Method Chaining for writing the programs. The Method Chaining is a practice of calling multiple methods in a single line in a chain. Many API’s such as Spring framework and Hibernate support Method Chaining style of programming.  The one common problem we find in this approach is, it becomes difficult to identify the source of NullPointerException.

For Example, consider the below code:

package com.techxposer.sample;

public class EmployeeService {
 
 
 public void processEmployee(Employee employee){
 
 employee.getAddress().getStreet();
 }
 
 public static void main(String s[]){
 
 try {
 new EmployeeService().processEmployee(null);
 }catch(NullPointerException e){
 System.out.println("Exception when employee is null");
 e.printStackTrace();
 }

 try {
 new EmployeeService().processEmployee(new Employee());
 }catch(NullPointerException e){
 System.out.println("\nException when employee is not null and address is null");
 e.printStackTrace();
 }
 }
}

When run this code we can find error message as shown in Figure-2 below:

Figure-2: NullPointerException before JDK-14

As shown in Figure-2 the information from JVM via NullPointerException is same in both the cases. Its really difficult for the developer to identify whether the source for the issue is employee or address references.

The JDK 14 solves this problem by enhancing the exception message. This includes additional details describing the source making it more helpful. The figure-3 shows NullPointerException messages when running the above code using JDK-14.

Figure-3: NullPointerException in JDK 14

Note: In JDK 14 also to get the detailed helpful NullPointerException message we need to run java application passing-XX:+ShowCodeDetailsInExceptionMessages.

java -XX:+ShowCodeDetailsInExceptionMessages com.techxposer.sample.EmployeeService

As shown in Figure-3, the exception message includes details of source such as <parameter1> i.e. employee is null in first case. In second case it details as the getAddress() returned null.

This can really help developers to pinpoint the source of NullPointerException in one go. 🙂

Few points to consider:

The helpful message with NullPointerException has few point to consider as described below:

  1. Only the NullPointerException’s created and thrown by JVM will include detail message, not the explicitly created and throw exception.
  2. The null-detail message is unable to show the actual array indices which led to a null element. For example, the run-time values of i when  getAddressLines()[i] is null in employee.getAddress().getAddressLines()[i].toLowerCase().
  3. The null-detail message is not reported for NullPointerException’s caused by code in hidden methods, which are special-purpose low-level methods generated and called by the JVM, e.g., to optimize string concatenation.
  4. The local variable names contained in detail message will be as in the source code if debug information is included in class file. (i.e. if compiled using -g). If we observe Figure-3 the detail message descried employee variable of source code as <parameter1>, if we recompile the code using -g we will see employee instead of <parameter1>.

Happy Coding 🙂