Fundamentals 6 min read

How Java 14’s Enhanced NullPointerException Supercharges Debugging

Java 14 introduces enhanced NullPointerException messages via JEP 358, allowing developers to pinpoint the exact variable that is null, with optional JVM flag activation, lazy computation, and considerations for debug information, ultimately speeding up debugging and improving code reliability.

Java Backend Technology
Java Backend Technology
Java Backend Technology
How Java 14’s Enhanced NullPointerException Supercharges Debugging

1. Traditional NullPointerException

When using chained calls, a NullPointerException can be thrown without indicating which variable is null. Example:

String city = employee.getDetailInfos().getRegistryAddress().getCity();

If employee, getDetailInfos() or getRegistryAddress() returns null, the JVM throws a NullPointerException, and only the method, file and line number are shown.

2. Enhanced NullPointerException

JEP 358, introduced in JDK 14, adds detailed messages that describe which variable is null. The feature was proposed in 2019 and shipped in October 2019. By default the detailed messages are disabled; they can be enabled with the JVM flag:

-XX:+ShowCodeDetailsInExceptionMessages

2.1 Detailed exception information

Running the previous code with the flag 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 tells us that the null value comes from getRegistryAddress(), saving debugging time.

3. Technical details

The detailed message is computed only when the JVM itself throws a NullPointerException; manually thrown exceptions are unaffected. The computation is lazy—it runs only when the message is printed—so there is no performance impact in normal execution.

If the code is compiled with the -g flag, the message may include local variable names, which could be a security concern because it reveals source‑level identifiers.

Employee employee = null;
employee.getName();

With debug information the exception contains the variable name employee; without it the JVM shows a generic placeholder like <local1>.

In summary, Java 14’s enhanced NullPointerException provides precise information about the null variable, allowing developers to locate bugs faster and improve code reliability.

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.

Javaprogramming fundamentalsnullpointerexceptionJEP358JDK14
Java Backend Technology
Written by

Java Backend Technology

Focus on Java-related technologies: SSM, Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading. Occasionally cover DevOps tools like Jenkins, Nexus, Docker, and ELK. Also share technical insights from time to time, committed to Java full-stack development!

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.