Understanding Enhanced NullPointerException in Java 14 (JEP 358)
The article explains how Java 14 introduces enhanced NullPointerException messages via JEP 358, how to enable them with the -XX:+ShowCodeDetailsInExceptionMessages flag, and discusses the technical details, benefits, and potential security considerations of this feature.
In Java, dealing with NullPointerException can be painful because the stack trace often does not reveal which variable was null . Since Java 14, JEP 358 provides more helpful exception messages that pinpoint the exact expression that caused the failure.
1. Traditional NullPointerException
When code uses chained calls, such as:
String city = employee.getDetailInfos().getRegistryAddress().getCity();if employee , getDetailInfos() or getRegistryAddress() returns null , the JVM throws a plain NullPointerException without indicating which part of the chain failed.
2. Enhanced NullPointerException
Originally implemented by SAP in 2006 and later adopted by the OpenJDK community, JEP 358 was released in JDK 14. It augments the exception message with the name of the variable or expression that evaluated to null . By default the detailed messages are disabled; they can be turned on with the JVM option:
-XX:+ShowCodeDetailsInExceptionMessages2.1 Detailed Exception Message Example
Running the previous code with the flag produces a message like:
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)This tells us that the RegistryAddress was missing, allowing faster debugging.
3. Technical Aspects
The enhanced message is generated only when the JVM itself throws the exception; manually thrown NullPointerException will not receive the extra details. The computation is lazy – the detailed message is built only when the exception's toString() is invoked, so there is no runtime overhead in normal execution.
Because the message may include local variable names, it can expose source‑level information when the code is compiled with the -g debug flag. Without debug information, the JVM falls back to generic identifiers such as <local1> .
In summary, Java 14’s enhanced NullPointerException helps developers locate the exact cause of a null reference quickly, improving debugging efficiency while introducing a minor security consideration when debug symbols are present.
Java Architect Essentials
Committed to sharing quality articles and tutorials to help Java programmers progress from junior to mid-level to senior architect. We curate high-quality learning resources, interview questions, videos, and projects from across the internet to help you systematically improve your Java architecture skills. Follow and reply '1024' to get Java programming resources. Learn together, grow together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.