Backend Development 6 min read

Enhanced NullPointerException in Java 14: Using JEP 358 for Precise Debugging

Java 14 introduces enhanced NullPointerException messages via JEP 358, allowing developers to pinpoint the exact null variable in chain calls, with optional activation through the ShowCodeDetailsInExceptionMessages flag and considerations of performance and security.

Architect's Tech Stack
Architect's Tech Stack
Architect's Tech Stack
Enhanced NullPointerException in Java 14: Using JEP 358 for Precise Debugging

In Java, NullPointerException (NPE) often makes debugging difficult, especially when using chained method calls. A typical example is retrieving an employee's city with a long chain of getters, where any intermediate null causes an NPE without indicating which variable is null.

Java 14 implements JEP 358, which enriches NPE messages by describing the exact variable that is null. The feature was originally introduced by SAP in 2006, proposed to OpenJDK in 2019, and shipped as default in JDK 14. By default the detailed messages are disabled; they can be enabled with the JVM flag -XX:+ShowCodeDetailsInExceptionMessages .

When the flag is active, running the same chained code produces an exception 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)

The message clearly identifies the null expression, saving developers time during debugging.

Technical details: the JVM only generates the detailed message when it itself throws the NPE; manually thrown NPEs retain their original messages. The computation of the detailed message is lazy—it occurs only when the message is printed, so there is no runtime performance penalty. However, the detailed message may expose local variable names if the code is compiled with the -g debug flag, which could be a security concern.

For example, with debug information the JVM can output:

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

Without debug info, the JVM falls back to a generic placeholder like <local1> .

Overall, Java 14’s enhanced NPE provides clearer diagnostics, helping developers locate the root cause of null references quickly while keeping performance impact minimal, though developers should be aware of the potential information leakage when debug symbols are present.

backendDebuggingJavanullpointerexceptionJava14JEP 358
Architect's Tech Stack
Written by

Architect's Tech Stack

Java backend, microservices, distributed systems, containerized programming, and more.

0 followers
Reader feedback

How this landed with the community

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