In the Java language, dealing with null Pointers is often a headache, and if you’re not careful, you could end up with an online Bug that will give you a 3.25 performance review. NullPointerException is a NullPointerException that can be handled by Java14.

1. Traditional NullPointerException

In our coding process, we often use the chain call method to write code, which is easy and clear to write, but when NullPointerException occurs, it’s very difficult to know when the exception started. For a simple example, such as the following code, to find the domicile of an employee in the company, we call

String city = employee.getDetailInfos().getRegistryAddress().getCity();
Copy the code

If employee, getDetailInfos(), or getRegistryAddress() is null during a chain call, the JVM throws a NullPointerException. What is the root cause of the exception? Without using a debugger, it is difficult to determine which variable is null. Also, the JVM simply prints the method, filename, and line number that caused the exception, and that’s it. Now, I’ll take you through how Java 14 solves this problem with JEP 358.

2. Enhanced NullPointerException

SAP implemented an enhanced version for its commercial JVMS in 2006

NullPointerException

As a result, this feature was completed and released in JDK 14 in October 2019
JEP 358

Null”
null
NullPointerException
null

Verbose exception messages are turned off by default in JDK 14

-XX:+ShowCodeDetailsInExceptionMessages
Copy the code

2.1 Detailed exception information

Consider in the case of activated ShowCodeDetailsInExceptionMessages sign again to run the code:

Exception in thread "main" java.lang.NullPointerException: 
  Cannot invoke "RegistryAddress.getCity()" because the return value of 
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$DetailInfos.getRegistryAddress()" is null
  at com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException.main(HelpfulNullPointerException.java:10)
Copy the code

This time, from the additional information, we know that the loss of the employee’s personal details to the registered address is causing our exception. The information gained from this enhancement can save us time for debugging.

The JVM consists of two parts for a detailed exception message. The first section represents the failed operation, which is referenced as

null

The second part identifies the results

null

reference

Cannot invoke "String.toLowerCase()" because the return value of "getEmailAddress()" is null
Copy the code

To generate exception messages, JEP 358 reconstructs a portion of the source code that pushes empty references onto the operand stack.

3. Technology

Now we have a good understanding of how to use enhanced

NullPointerException
null

First, only if the JVM itself throws one

NullPointerException

Second, **JEP 358 ** calculates messages lazily, which means that the enhanced NullPointerException is called only when we print an exception message, not when it occurs. Therefore, there should be no performance impact on the normal JVM flow, where we can catch and rethrow exceptions, because we don’t just want to print exception messages.

Finally, detailed exception messages may contain local variable names in the source code. Therefore, we can consider this a potential security risk. However, only active ones are used in the run

-g

Employee employee = null;
employee.getName();
Copy the code

When the above code is executed, the local variable name is printed in the exception message:

"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$Employee.getName()"
because "employee" is null
Copy the code

Instead, without additional debugging information, the JVM provides only the variables it knows in the verbose message:

Cannot invoke 
  "com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$Employee.getName()"
because "<local1>" is null
Copy the code

The JVM prints the compiler-assigned variable index instead of the local variable name (EMPLOYEE).

This is the end of NullPointerException processing. With Java14 enhanced NullPointerException, we can quickly locate the cause of code problems, debug the code faster, save time and improve efficiency.