NullPointerException
Java Programming Language
Severity: ModerateWhat Does This Error Mean?
A NullPointerException means your code tried to use an object that has no value — it is null. Think of it like trying to open a door that does not exist. You have a variable that should point to an object, but it points to nothing instead. This is one of the most common Java errors beginners and experienced developers both encounter.
Affected Models
- Java 8
- Java 11
- Java 17
- Java 21
- All Java versions
Common Causes
- Calling a method on a variable that was declared but never assigned a real object
- A method returned null and you used the result without checking for null first
- An item in an array or list is null and you tried to call a method on it
- A field in a class was never initialized before being used in a method
- Chaining method calls (like a.getB().getC()) when one of the middle calls returns null
How to Fix It
-
Read the full stack trace in your console or log. It shows the exact file name and line number where the null was used.
Example: 'at com.example.MyClass.doSomething(MyClass.java:42)' means line 42 of MyClass.java is where the crash happened.
-
Go to that line and identify which variable could be null. Ask: was this variable ever assigned a real object, or could the method that returned it have returned null?
In Java 14 and newer, the error message often says exactly which variable was null — for example: 'Cannot invoke String.length() because str is null'.
-
Add a null check before using the variable. Use an if statement: if (myObject != null) { ... } before calling any methods on it.
Or use Objects.requireNonNull(myObject, 'myObject must not be null') at the start of a method to catch the problem early with a clear message.
-
If the variable should never be null, trace back to where it is set and find why it ended up as null. Check database queries, API responses, or method return values.
Use Optional<T> as a return type for methods that might legitimately return no value — it forces callers to handle the empty case.
-
Consider using Java's Optional class or a null-safe library to make null handling more explicit and readable throughout your codebase.
Optional.ofNullable(value).ifPresent(v -> doSomething(v)) is a clean way to handle potentially-null values without verbose if checks.
When to Call a Professional
NullPointerExceptions are almost always fixable by the developer — they are not a sign of hardware problems or system failure. The stack trace tells you exactly which line caused the error. Since Java 14, the error message even tells you which variable was null — read it carefully.
Frequently Asked Questions
Why does Java not just use a default empty object instead of null?
Java uses null as a deliberate signal meaning 'no value here'. It is up to the developer to decide what 'no value' means for their program — sometimes you want to show an error, sometimes you want to use a default. Java cannot make that choice for you automatically.
How do I find which variable is null when I have a long chain like a.getB().getC().getName()?
Break the chain into separate lines, one call per line, and assign each result to a variable. Then add null checks after each step. This also makes the stack trace point to the exact step that failed — instead of just the long chain line.
Is NullPointerException a checked or unchecked exception?
It is unchecked — it extends RuntimeException. This means Java does not force you to catch it or declare it. That is why it often surprises developers at runtime rather than being caught at compile time.