GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing javaevolved.dev — Helpful NullPointerExceptions | java.evolved
Errors
Beginner
Helpful NullPointerExceptionsJVM automatically tells you exactly which variable was null. Code Comparison
✕ Java 8
// Old NPE message: // "NullPointerException" // at MyApp.main(MyApp.java:42) // Which variable was null?!
✓ Java 14+
// Modern NPE message: // Cannot invoke "String.length()" // because "user.address().city()" // is null // Exact variable identified!
See a problem with this code? Let us know.
Why the modern way wins
🔍
Exact variableThe message names the null variable in the chain. ⚡
Faster debuggingNo more guessing which of 5 chained calls was null. 🆓
Free upgradeNo code changes — just run on JDK 14+. Old Approach
Cryptic NPE
Modern Approach
Detailed NPE
Since JDK
14
Difficulty
Beginner
JDK Support
Helpful NullPointerExceptions
Available
Widely available since JDK 14 (March 2020) How it works
Helpful NPEs describe which expression was null and what operation failed. This is enabled by default since Java 14 — no code change needed, just upgrade the JDK. Related Documentation
Related patterns
Errors
Beginner
Null case in switchJava 8
// Must check before switch
if (status == null) {
return "unknown";
}
return switch (status) {
case ACTIVE -> "active";
case PAUSED -> "paused";
default -> "other";
};
Java 21+
return switch (status) {
case null -> "unknown";
case ACTIVE -> "active";
case PAUSED -> "paused";
default -> "other";
};
JDK 21+
→
Errors
Beginner
Multi-catch exception handlingPre-Java 7
try {
process();
} catch (IOException e) {
log(e);
} catch (SQLException e) {
log(e);
} catch (ParseException e) {
log(e);
}
Java 7+
try {
process();
} catch (IOException
| SQLException
| ParseException e) {
log(e);
}
JDK 7+
→
Errors
Beginner
Objects.requireNonNullElse()Java 8
String name = input != null
? input
: "default";
// easy to get the order wrong
Java 9+
String name = Objects
.requireNonNullElse(
input, "default"
);
JDK 9+
→
|
| You are visitor 01402503 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |