Scrivio Interview library
DebuggingFoundations5 min read · updated 12 November 2026

This method throws NullPointerException on the return line in production. Why?

Java
Map<String,Integer> counts = ...;
int get(String k) {
return counts.get(k);
}

counts.get(k) returns null when the key is missing, and because the method returns int, Java auto-unboxes that null by calling intValue() on it, which throws NullPointerException. We fix it with counts.getOrDefault(k, 0) or an explicit null check. Recent JDKs print a helpful NullPointerException message that names the failing intValue() call, which makes this kind of bug quick to locate.

Why this question is asked

NullPointerExceptions on a line that contains no visible dereference confuse many developers, and this is the most common way to produce one. The line return counts.get(k) has no dot after the call, yet it still fails. Interviewers ask it because the answer shows whether we understand that unboxing is a hidden method call, and whether we know the other places the same trap appears. In this answer, we will recap how Map.get and unboxing work, reproduce the exception and read its message, fix it, and then look at two related traps: ternary expressions and arithmetic on wrapper fields.

1A Quick Recap: Map.get and Unboxing

A Map<String, Integer> stores Integer objects, not primitive ints, because generics only work with objects. Map.get returns the value for a key, or null when the key is not present. Null is a perfectly valid Integer reference.

Unboxing is the automatic conversion from Integer to int. The compiler inserts it whenever an Integer is used where an int is required, for example as the return value of a method declared to return int. Unboxing is not magic: it is a call to intValue() on the object. And calling any method on null throws NullPointerException.

2Reproducing the Exception

Here is the method with the hidden call written out, and what happens for a missing key.

Java
Map<String, Integer> counts = new HashMap<>(Map.of("apple", 3));
int get(String k) {
return counts.get(k); // compiled as: return counts.get(k).intValue();
}
get("apple"); // 3
get("pear"); // NullPointerException
Step throughreturn counts.get("pear") in an int method
  1. Look up "pear"the key is not in the map
  2. Map.get returns nulla valid Integer reference
  3. Return type is intthe compiler inserted intValue()
  4. null.intValue()NullPointerException

4 steps in order. Play them, or step through them one at a time.

Since JDK 14, and on by default since JDK 15, the JVM produces helpful NullPointerException messages that describe exactly what was null. For this line the message reads like this: Cannot invoke "java.lang.Integer.intValue()" because the return value of "java.util.Map.get(Object)" is null. That message names both the unboxing call and the method that returned null, which tells us immediately that this is an unboxing problem rather than a null map.

3Fixing It

The right fix depends on what a missing key means. If a missing word simply has a count of zero, getOrDefault says so in one call. If a missing key is a real error, we check for null and fail with a clear message. If the caller should decide, we return Integer or OptionalInt and let the type show that the value may be absent.

Java
int get(String k) {
return counts.getOrDefault(k, 0); // "pear" -> 0
}
int getRequired(String k) {
Integer v = counts.get(k);
if (v == null) throw new IllegalArgumentException("unknown key: " + k);
return v; // safe: v is not null here
}
// When building the counts, merge avoids the same trap:
counts.merge("pear", 1, Integer::sum); // pear -> 1, no null handling needed

Notice the last line. A common way to build such a map is counts.put(k, counts.get(k) + 1), which fails with the same exception the first time a key appears. Map.merge inserts the initial value when the key is missing and combines otherwise, so there is no null to unbox.

4The Same Trap in Ternaries and Arithmetic

Unboxing also happens in places that look even more innocent. In a conditional expression, if one branch is a primitive int and the other is an Integer, the whole expression has type int, so the Integer branch is unboxed, even when the result is assigned to an Integer variable. Arithmetic on wrapper fields unboxes too.

Java
Integer nullable = null;
boolean flag = false;
Integer x = flag ? 1 : nullable; // NullPointerException: nullable is unboxed to int
Integer y = flag ? Integer.valueOf(1) : nullable; // null: both branches are Integer
class Line { Integer quantity; }
Line line = new Line();
int total = line.quantity * 2; // NullPointerException: quantity is null

In the first case, the compiler sees int and Integer, applies the rules for numeric conditional expressions, and unboxes nullable before boxing the result again. Making both branches Integer keeps the expression a reference type, so null passes through. These cases show why wrapper types should be used only where null is meaningful, and handled deliberately where they are.

5Follow-up questions

Why can Integer x = flag ? 1 : nullableInteger; throw an NPE?

Because one operand is the int literal 1 and the other is an Integer, the conditional expression is a numeric one with type int. Java therefore unboxes nullableInteger by calling intValue() on it, and then boxes the int result again to assign it to x. When flag is false and nullableInteger is null, the unboxing throws. Writing Integer.valueOf(1) in the first branch, or using an if statement, keeps the expression typed as Integer and avoids it.

Does the same happen with a Map of Long or Double?

Yes. Any wrapper unboxed from null throws, whether the call is longValue(), doubleValue() or booleanValue(). Map<String, Boolean> is a common one, because if (flags.get(name)) unboxes a null for a missing flag.

Should we turn off helpful NullPointerException messages in production?

They are on by default and generally worth keeping, because they make logs far more useful. The message is computed only when it is requested, so the cost is small. In very sensitive environments, teams sometimes review whether the message could reveal internal names, and the -XX:-ShowCodeDetailsInExceptionMessages option turns them off.

6Conclusion

In this answer, we saw that Map.get returns null for a missing key, and that returning it from an int method inserts a hidden intValue() call that throws NullPointerException. Helpful NPE messages name that call and the method that returned null. We fixed it with getOrDefault, an explicit null check or merge when building the map, and saw the same unboxing trap in ternaries that mix int and Integer and in arithmetic on wrapper fields.

What to say in the interviewAbout 80 seconds aloud

Open with

The map stores Integer objects, and Map.get returns null when the key is missing. The method is declared to return int, so the compiler inserts auto-unboxing, which is really a call to intValue on the returned Integer.

Then, if they want more

For a missing key that is intValue on null, so it throws NullPointerException on the return line even though there is no visible dereference. On a recent JDK the helpful NullPointerException message says exactly that: it cannot invoke Integer.intValue because the return value of Map.get is null, which points straight at the unboxing.

The fix depends on what a missing key means. If it means zero, I would use counts.getOrDefault(k, 0). If it is an error, I would read into an Integer, check for null and throw a meaningful exception. If callers need to know, I would return Integer or OptionalInt.

When building such a map I would use merge with Integer::sum rather than get plus one. I would also mention the related traps: a ternary like flag ? 1 : nullableInteger has type int because one branch is primitive, so it unboxes and throws for null, and arithmetic on nullable wrapper fields unboxes too.

In general I keep wrappers for values where null has a meaning, and handle that null deliberately.

About this answer

Last updated
12 November 2026
Versions it names
Java 14, Java 15
Checked
Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
Where the question comes from
Written by Scrivio for practice. It is not taken from an interview.
Report an issue with this answer

Copy the line below and send it to the author with what you found. It carries the address of this page.

Issue with "This method throws NullPointerException on the return line in production. Why?": https://scrivio-interview-library.netlify.app/java/types-and-values/e3c23b6c83/