-
Notifications
You must be signed in to change notification settings - Fork 25
Expand file tree
/
Copy pathhelpful-npe.yaml
More file actions
48 lines (48 loc) · 1.4 KB
/
Copy pathhelpful-npe.yaml
File metadata and controls
48 lines (48 loc) · 1.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
---
id: 64
slug: "helpful-npe"
title: "Helpful NullPointerExceptions"
category: "errors"
difficulty: "beginner"
jdkVersion: "14"
oldLabel: "Java 8"
modernLabel: "Java 14+"
oldApproach: "Cryptic NPE"
modernApproach: "Detailed NPE"
oldCode: |-
// Old NPE message:
// "NullPointerException"
// at MyApp.main(MyApp.java:42)
// Which variable was null?!
modernCode: |-
// Modern NPE message:
// Cannot invoke "String.length()"
// because "user.address().city()"
// is null
// Exact variable identified!
summary: "JVM automatically tells you exactly which variable was null."
explanation: "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."
whyModernWins:
- icon: "🔍"
title: "Exact variable"
desc: "The message names the null variable in the chain."
- icon: "⚡"
title: "Faster debugging"
desc: "No more guessing which of 5 chained calls was null."
- icon: "🆓"
title: "Free upgrade"
desc: "No code changes — just run on JDK 14+."
support:
state: "available"
description: "Widely available since JDK 14 (March 2020)"
prev: "io/deserialization-filters"
next: "errors/optional-chaining"
related:
- "errors/null-in-switch"
- "errors/multi-catch"
- "errors/require-nonnull-else"
docs:
- title: "Helpful NullPointerExceptions (JEP 358)"
href: "https://openjdk.org/jeps/358"