A NullPointerException (NPE) in Java is thrown when code tries to use a reference that points to null as if it were a real object, for example by calling a method on it or reading a field. To fix a NullPointerException, read the stack trace to find the line, work out which reference was null, and then either make sure it is initialised or check and handle null before use.
Modern JVMs make this easier with helpful NullPointerException messages that name the exact variable. This guide covers the common causes and the fixes.
A reference variable can hold either an object or null. Using null where an object is needed throws the exception. The JVM raises it when you:
null;null;length of a null array or access an element of it;null wrapper such as Integer into an int;synchronized on null;throw null.Since Java 15 the JVM describes the failing expression by default. The message above says that String.length() was invoked on something that was null. Compile with javac -g to keep local variable names, and the message then shows name instead of <local1>.
The first line of the trace that belongs to your code, NpeDemo.main(NpeDemo.java:4), is where to look. For chained calls such as order.getCustomer().getAddress().getCity(), the message tells you which link returned null, for example because the return value of "Customer.getAddress()" is null.
Object fields default to null. If you forget to assign them, the first use fails.
Fix it by initialising the field, and make it final when it never changes.
Many APIs return null to mean "not found", for example Map.get. Check the result before you use it.
Assigning a null Integer to an int throws an NPE, which surprises many beginners.
Use Integer only when null is meaningful, and otherwise prefer the primitive.
Calling equals on a possibly null value fails. Put the known value first, or use Objects.equals.
Objects.requireNonNull(value, "value must not be null"). This fails early with a clear message instead of far away.null.Optional for return values that may be absent, but not for fields or method parameters.Objects.requireNonNullElse(value, fallback) to supply a default.@Nullable or @NonNull so tools and IDEs can warn you.Almost never. An NPE normally points to a bug, and catching it hides the cause. Fix the code that produced the null. The exception to this rule is a top-level handler that logs the failure for a request and keeps the server running.
Unchecked. It extends RuntimeException, so the compiler does not require you to catch or declare it.
Look for unboxing, a chained call returning null, or an array or collection element that is null. The helpful message in Java 15 and later names the exact expression.
No. It makes absence explicit for return values, but a method can still return null or an Optional itself can be null. Use it consistently and never return null from a method that returns Optional.
Reject null at the boundary with Objects.requireNonNull, return empty collections, and keep fields final and initialised. Fewer places then ever see a null.
A NullPointerException means a null reference was dereferenced. Read the stack trace and the helpful message to locate the reference, then fix its source: initialise fields, avoid returning null, watch unboxing, and compare strings safely. Validate early with Objects.requireNonNull and use Optional for absent results.