Skip to main content
PreparationZone Logo
PreparationZone
Back to Java

Java · Easy · Free

Helpful NullPointerExceptions — how to read the detail message in production?

JavaCore JavaFresher
Model answer

Helpful NullPointerExceptions

JDK 14+ enriches NPE messages with which variable was null in the expression chain—dramatically faster root cause in logs without debugger.

  • Enabled by default; disable with -XX:-ShowCodeDetailsInExceptionMessages if info leakage concern.
  • Shows method, field, and array index in chain.
  • Still fix null safety—don't rely on messages in API contracts.
  • Works with records and compact stack traces.
java
// NPE message example:
// Cannot invoke "String.length()" because "user.getName()" is null
Production: pair with structured logging of user id—not the null field alone for PII.