JDK 14 New Feature to print a detailed message in NullPointerExceptions.

Murali krishna Konduru
3 min readSep 12, 2020

In Object-oriented programming, a null refers to an object without any reference. Developers have to ensure that an object should not be null to access members and methods, else JVM throws NullPointerExceptions.

Every Java Programmer experienced the NullPointerExcepton in their programming times. In older versions, it leads to confusion to point the exact reason for the exception.

In Java 14 provided the new option to provide more helpful NullPointerException Messages. The main goal of this feature is to give detailed information about exactly which variable causes the exception.

This feature was first developed by SAP company. Later they proposed to Open JDK community, subsequently, it became part of JEP.

By default this option is disabled, to enable it we need to add VM arguments.

Adding VM argument in Eclipse.

To enable it, add the below command as VM argument.

  • XX:+ShowCodeDetailsInExceptionMessages

In general NullPointerException throws in the below cases.

Case 1: While accessing or modifying the field of a null object.

public class TestNullPointerException {public static void main(String[] args) {
Person person = null;
person.firstName = “Thomas”;
}
private class Person{
String firstName = null;
String secondName = null;
}
}

The JVM will point out the method, filename, and line number that caused the NullPointerException.

Exception in thread “main” java.lang.NullPointerException
at com.java.test.TestNull.main(TestNull.java:7)

In the above piece of code, the developer can locate person.firstName = “Thomas”; and infer that the variable person must have been null.

Case 2: In another case, in complex scenarios, it is difficult for developers to locate exactly which variable was null.

package com.java.test;public class TestNullPointerException {public static void main(String[] args) {
Person person = null;
person.address.contactDetails.mobileNumber = “9999999999”;
}
}
class Person{
String firstName = null;
String secondName = null;
Address address = null;
}

class Address{
String streetName = null;
String cityName = null;
ContactDetails contactDetails = null;
}

class ContactDetails{
String mobileNumber = null;
String emailAddress = null;
}

Exception in thread “main” java.lang.NullPointerException at com.java.test.TestNullPointerException.main(TestNullPointerException2.java:7)

The filename and line number do not pinpoint exactly which variable was null. In the above scenario, we can not determine which Object is the null person or address or contactDetails.

Case 3: Assigning one object to another with a null reference.

person.firstName = employee.firstName;

The filename and line number do not pinpoint the offending access path. Whether the person null or employee?

Case 4: Finally, an NPE could stem from a method call. Suppose an NPE occurs in this.

getAddress().getCommunication().mobileNumber = “9999999999”;

The filename and line number do not pinpoint which method call returned null. Whether it is getAddress() or getCommunication()?

Case 5: Taking the length of null for an array or it is a size of a null collection object like List.

String[] array = null;
array.length;
List arrayList = null;
arrayList.size();

How the null message will be computed.

By analyzing the byte code instructions, JVM determines which variable causes an exception and displays a detailed message with a variable name which is null in the NullPointerException along with File Name, method, and line number.

In the above piece of code, JVM looks at each object and which is having a null reference.

In the above Case 1,

Java 14 exception message will be more precise saying person object is null.

Exception in thread “main” java.lang.NullPointerException: Cannot assign field “firstName” because “person” is null at com.java.test.TestNull.main(TestNull.java:7)

Case 2: The exception message clearly determining the person Object is null.

Exception in thread "main" java.lang.NullPointerException: Cannot read field "address" because "person" is null at com.java.test.TestNullPointerException2.main(TestNullPointerException2.java:7)

Note: It is not applicable to customized exceptions. applicable only predefined exceptions.

This style of a message helps developers to pinpoint exactly which Object is causing NullPointerException and root cause without the help of additional tools and debuggers.

--

--