The Most Common Java Error
A NullPointerException (everyone calls it an NPE) happens when you try to use a reference that points to nothing - null - as though it pointed to a real object. Java variables of an object type either hold an object or hold null, the "no object here" value. The moment you ask null to do something - call a method on it, read one of its fields, index into it - there is nothing there to act on, so the JVM throws.
Unlike try-catch, which you reach for on the previous page to handle expected failures, an NPE is almost always a plain bug. The goal of this page is not to catch them - it is to understand why they happen and write code that does not produce them.
There is no String for length() to run on, so the program stops with a NullPointerException.
Reading the Message
Since Java 14, the message tells you exactly what was null - this is called the Helpful NullPointerException. Read it before changing anything:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is null
at Main.main(Main.java:4)
Two pieces matter. Cannot invoke "String.length()" is the operation that failed, and because "name" is null names the culprit. The at Main.main(Main.java:4) line is the stack trace pointing at the exact line. So the fix is not "wrap line 4 in a try" - it is "figure out why name is null on line 4". Almost always the bug is somewhere earlier, where the value should have been assigned and was not.
The Ways You Trigger One
NPEs come from a handful of operations, all variations on "touching null":
Map lookups are a classic source: get returns null when the key is absent, and the NPE often surfaces lines later when you finally use that value. Unboxing is the sneaky one - assigning a null Integer to an int throws, because there is no number to copy.
Guarding With a Null Check
The simplest defense is an if that confirms a reference is non-null before you use it:
When you compare a variable against a constant String, put the constant first - "yes".equals(answer) instead of answer.equals("yes"). If answer is null, the first form returns false calmly while the second throws.
Fail Fast With Objects.requireNonNull
Scattering null checks everywhere gets noisy. When a value should never be null - like a constructor argument - validate it at the boundary with Objects.requireNonNull. It throws immediately, with a clear message, at the point the bad value arrives instead of deep inside your code later:
This "fail fast" habit turns a vague NPE 200 lines away into a precise complaint at the source. Catching the exception here is only to show the message - in real code you would let it surface so the bug gets fixed.
Avoiding Nulls in the First Place
The best NPE is one that can never happen because there is no null to hit. A few habits go a long way:
- Return an empty collection or string, never
null.Collections.emptyList()and""are safe to loop over and call methods on. - Use
getOrDefaulton maps so a missed lookup yields a real value instead ofnull. - Initialize fields when you declare them rather than leaving them
nulluntil "later".
When a value is genuinely optional - a lookup that may legitimately find nothing - Java offers Optional, a container that forces the caller to handle the "absent" case instead of silently handing back a null. That is the related concept to read next if you want to design these gaps out of your APIs entirely.
Wrapping Up
A NullPointerException is Java telling you that you used a reference holding null as if it held an object. The fix is rarely to catch it - it is to read the helpful message, trace back to where the value should have been set, and either guarantee it is non-null or guard the spot where you use it. Lean on Objects.requireNonNull to fail fast at boundaries, prefer empty values and getOrDefault over null, and reach for Optional when absence is a real, expected outcome. Master that mindset and the most common error in Java becomes one of the rarest in your own code.
Frequently Asked Questions
What causes a NullPointerException in Java?
It happens when you use a reference that points to null as if it pointed to a real object - for example calling a method (name.length()), reading a field, accessing an array element, or unboxing a null Integer. The variable holds no object, so there is nothing to act on and the JVM throws a NullPointerException.
How do you fix a NullPointerException in Java?
Read the message - since Java 14 it names exactly what was null (e.g. "Cannot invoke "String.length()" because "name" is null"). Then trace why that variable is null: a missing initialization, a method that returned null, or a map lookup that missed. Fix the source so the value is never null, or guard the use with a null check, Objects.requireNonNull, or Optional.
Is it better to check for null or catch a NullPointerException?
Check for null. A NullPointerException signals a bug in your logic, not an expected condition, so you should prevent it rather than catch it. Catching one hides where the real problem is. Reserve try/catch for genuinely exceptional situations like I/O failures.