How Java 14’s Enhanced NullPointerException Simplifies Debugging
This article explains how Java 14’s JEP 358 introduces enhanced NullPointerException messages that pinpoint the exact null variable, shows how to enable the feature, and demonstrates its impact on debugging with practical code examples.
In Java, dealing with NullPointerException (NPE) can be painful, often leading to hard‑to‑track bugs and performance penalties.
Java 14 introduces a better way to handle NPEs through JEP 358, which provides detailed exception messages that identify the exact variable that was null.
Traditional NullPointerException
When using chained method calls, a null value anywhere in the chain triggers an NPE, but the stack trace only shows the method, file, and line number, making it difficult to locate the offending variable.
String city = employee.getDetailInfos().getRegistryAddress().getCity();If employee, getDetailInfos(), or getRegistryAddress() returns null, the JVM throws an NPE.
Enhanced NullPointerException
Originally implemented by SAP in 2006, the enhanced NPE was proposed to OpenJDK in 2019 and shipped as part of JDK 14.
JEP 358 improves the readability of NPE messages by describing which variable is null alongside the method, file, and line number. The feature is disabled by default and can be enabled with the JVM option: -XX:+ShowCodeDetailsInExceptionMessages When enabled, running the earlier code produces a detailed message such as:
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 null, saving debugging time.
Technical Details
The JVM only computes the detailed message when it itself throws an NPE; manually thrown NPEs retain their original messages.
Message generation is lazy: the detailed message is built only when Throwable.getMessage() is called, so there is no performance impact during normal execution.
If the code is compiled with the -g flag, local variable names may appear in the message, which could be a security concern. Without debug information, the JVM reports the compiler‑generated variable index instead. Employee employee = null; employee.getName(); With debug info, the exception might read:
"com.developlee.java14.helpfulnullpointerexceptions.HelpfulNullPointerException$Employee.getName()" because "employee" is nullWithout debug info, it reports a generic placeholder like <local1>.
In summary, Java 14’s enhanced NullPointerException provides precise information about null references, allowing developers to locate and fix bugs faster, improving debugging efficiency.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Programmer DD
A tinkering programmer and author of "Spring Cloud Microservices in Action"
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.
