How Java 14’s Enhanced NullPointerException Simplifies Debugging

Java 14’s enhanced NullPointerException, introduced via JEP 358, provides detailed messages that pinpoint the exact null variable in a stack trace, dramatically simplifying debugging of chained calls and reducing time spent hunting down elusive bugs.

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

In Java, NullPointerException (NPE) is a common source of bugs, and Java 14 introduces enhancements that make NPE messages more informative.

1. Traditional NullPointerException

When using chained calls, a null value anywhere in the chain triggers a generic NPE, making it hard to locate the exact cause.

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

If employee, getDetailInfos() or getRegistryAddress() returns null, the JVM throws an NPE without indicating which variable is null.

2. Enhanced NullPointerException (JEP 358)

JEP 358, completed in October 2019 and shipped in JDK 14, augments NPE messages by describing which variable is null. The detailed message is disabled by default and can be enabled with the JVM option:

-XX:+ShowCodeDetailsInExceptionMessages

2.1 Detailed exception information

With the flag enabled, running the previous code produces output 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 pinpointed message shows that the null value originates from the missing registry address.

3. Technical aspects

The enhanced message is generated only when the JVM itself throws an NPE; manually thrown NPEs retain their original messages. The computation is lazy, occurring only when the exception message is printed, so there is no performance impact during normal execution.

Because the detailed message may include local variable names, it can expose source information when the code is compiled with the -g debug flag, which is a potential security concern.

Overall, Java 14’s enhanced NullPointerException helps developers quickly identify the null source, speeding up debugging and improving productivity.

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.

Javanullpointerexceptionjava14JEP 358
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.