Autoboxing NullPointerException
Fast recall
Quick revision
Core answer
null wrapper object to a primitive, it calls .intValue() (or similar) on null — which throws a NullPointerException. This happens silently in arithmetic expressions, method calls, return statements, and switch statements involving Integer, Long, or other wrapper types.Whether you can identify the subtle unboxing NPE — it doesn't look like a null access from the code, which is what makes it tricky.
Thinking NPE from autoboxing requires explicit method calls on null. It happens implicitly wherever a wrapper is used where a primitive is expected.
Give a real-world example: a method returning Integer can return null, and if you assign it directly to int the caller crashes. Or a HashMap.get() returns Integer — if the key is absent you get null, and using it in arithmetic blows up.
Interview-ready guide
Interview answer
What makes this bug nasty is that it doesn't look like a null bug. There's no . operator on the line, no obvious method call — just something like int total = getScore() where getScore() returns an Integer. The compiler silently turns that into getScore().intValue(), so when the method hands back null, you get a NullPointerException on a line that reads like a simple assignment.
The most common place I've seen this is maps. map.get(key) returns null when the key is absent, and if you assign it straight to an int, it unboxes null and blows up. The fix is map.getOrDefault(key, 0), which never returns null. It also hides in arithmetic — Integer a = null; a + 5 fails on the + because the operator needs the primitive first — and in switch, where a null Integer is unboxed before any case is even checked.
Any wrapper used where a primitive is expected is a potential
NullPointerException.
So the way I defend against it: null-check before unboxing, lean on getOrDefault() for maps, or use Objects.requireNonNullElse(n, 0) from Java 9. And where I control the API, I avoid returning a null Integer in the first place — a sentinel or OptionalInt saves every caller from this trap.
Autoboxing NullPointerException
Direct Answer
When Java unboxes a null wrapper object to a primitive, it calls .intValue() (or similar) on null — which throws a NullPointerException. This happens silently in arithmetic expressions, method calls, return statements, and switch statements involving Integer, Long, or other wrapper types.
Code Example
import java.util.HashMap;
public class AutoboxNPE {
static Integer getScore(boolean found) {
return found ? 95 : null; // returns null when not found
}
public static void main(String[] args) {
// Case 1: method returns Integer, assigned to int
int score = getScore(false); // NPE — compiler: getScore(false).intValue() → null.intValue()
// Case 2: HashMap.get() returns null for absent key
HashMap<String, Integer> map = new HashMap<>();
map.put("Alice", 90);
int bobScore = map.get("Bob"); // NPE — Bob not in map, get() returns null
// Safe fix: use getOrDefault
int bobSafe = map.getOrDefault("Bob", 0); // 0 — no NPE
// Case 3: arithmetic with null wrapper
Integer a = null;
// int sum = a + 5; // NPE — compiler: a.intValue() + 5 → null.intValue()
// Case 4: switch on null Integer
Integer x = null;
// switch (x) { ... } // NPE — switch unboxes x first
// Safe pattern: always null-check before unboxing
Integer n = getScore(false);
int value = (n != null) ? n : 0; // null-safe
// Or use Objects.requireNonNullElse
int safe = java.util.Objects.requireNonNullElse(n, 0); // Java 9+
}
}
Common Mistakes & Pitfalls
- Thinking the NPE needs a visible method call — there's no
.onint x = getValue(), yet the hiddenintValue()is exactly where it throws. - Using
map.get(key)straight as anint— a missing key returnsnull, which unboxes to aNullPointerException; reach forgetOrDefault(key, 0)instead. - Forgetting
switchunboxes first —switch (nullInteger)throws before anycaseruns, so null-check the wrapper before switching on it. - Returning a
nullIntegerfrom an API — it pushes the unboxing trap onto every caller; prefer a sentinel value, a primitive return, orOptionalInt.
Check your understanding
Answer these without looking back at the article.
- What is the Integer cache in Java?
- How does getOrDefault() prevent NullPointerException when using HashMap?
- What is Objects.requireNonNullElse() in Java 9?
Looking for a structured roadmap?
Every module on this page is also part of the full Java backend interview track.