Home โบ Java exceptions โบ NullPointerException
NullPointerException: causes and fixes
A null reference was used where an object was needed: a method call, field access, unboxing or array access on null.
What it looks like
java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is nullExtends RuntimeException in java.lang.
What it means
The JVM throws NullPointerException when code treats null as if it were an object: calling a method on it, reading or writing a field, taking the length of a null array, throwing null, or unboxing a null Integer into an int. Since Java 14 the message says exactly which expression was null, for example because "order.getCustomer()" is null, which usually points straight at the bug.
Common causes
- A method returned null (a map lookup, a repository
findBythat found nothing, a JSON field that was missing). - A field was never initialised, or a dependency was not injected because the object was created with
newinstead of by Spring. - Auto-unboxing of a null wrapper, such as
int x = map.get(key)when the key is absent. - A chain such as
a.getB().getC()where any link can be null.
How to fix it
- Read the helpful message first: it names the null variable or method call.
- Return
Optionalfrom methods that can legitimately have no result, and handle the empty case. - Validate arguments at the start of a method with
Objects.requireNonNull(x, "x")so the failure is near the cause. - Initialise collections and fields, and let Spring create beans instead of calling
newon them.
Broken and fixed
String name = map.get("user");
int len = name.length(); // NPE when "user" is absentString name = map.getOrDefault("user", "");
int len = name.length();๐ Unravel the failing code ๐ Unravel the fix
In Spring and enterprise code
In Spring, a null autowired field almost always means the object was not created by the container (a new call, or a class outside component scanning). In JPA, a null association can mean it was never loaded or set.
Debug it faster
Paste the whole stack trace into the stack trace beautifier to hide framework frames, jump to the root cause and find the first line of your own code. It runs in your browser, so the trace never leaves your machine.
Frequently asked questions
Is NullPointerException a checked or unchecked exception?
NullPointerException is an unchecked exception (it extends RuntimeException), so you are not forced to catch it. Prevent it with validation, or handle it where the input enters the system.
Which package is NullPointerException in?
It lives in java.lang, so the import is import java.lang.NullPointerException; (java.lang needs no import).
What is the first thing to check when I see NullPointerException?
Read the message and the first stack trace line in your own code. The usual causes are: A method returned null (a map lookup, a repository findBy that found nothing, a JSON field that was missing); and A field was never initialised, or a dependency was not injected because the object was created with new instead of by Spring.