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.

Programmer DD
Programmer DD
Programmer DD
How Java 14’s Enhanced NullPointerException Simplifies Debugging

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 null

Without 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.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

Javanullpointerexceptionjava14JEP358
Programmer DD
Written by

Programmer DD

A tinkering programmer and author of "Spring Cloud Microservices in Action"

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.