NullPointerException Java Origin: Application & API Last updated 15 Oct 2026

java.lang.NullPointerException

Code called a method or accessed a field on a reference that is null.

Exact message you may see Attempt to invoke virtual method on a null object reference
Seen on: Java Android

Meaning

Since Java 14, helpful NPE messages tell you exactly what was null: Cannot invoke "String.length()" because "name" is null. Read that message and the first stack frame in your own package.

Common causes

  • Uninitialized field or variable
  • Method returned null (map lookup, repository find)
  • Dependency injection didn’t populate a field (@Autowired on a new-created object)
  • Auto-unboxing a null Integer to int
  • Android: view not found (findViewById returns null) or used after destroy

⚡ Quick fix

  1. Read the “because X is null” part of the message
  2. Find the first stack frame in your code
  3. Return Optional or empty collections instead of null
  4. Validate inputs with Objects.requireNonNull(x, "x")

Detailed fix by platform

Java

  1. Optional instead of null checks:
    java
    String city = Optional.ofNullable(user)
            .map(User::getAddress)
            .map(Address::getCity)
            .orElse("Unknown");

Android

  1. In Kotlin, prefer non-null types and ?./?:; in Java, annotate with @Nullable/@NonNull so lint warns you.

Code examples

Helpful NPE message (Java 14+)

text
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "String.length()" because "name" is null
    at com.example.App.greet(App.java:12)

How to diagnose

  1. Message — What is null (Java 14+ tells you)?
  2. Frame — First frame in your package
  3. Origin — Where should the value have been set?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

Call to member function on null — Fatal error: Uncaught Error: Call to a member function x() on null (PHP)Call to member functi…ClassNotFoundException — java.lang.ClassNotFoundException / NoClassDefFoundError (Java)ClassNotFoundExceptionConcurrentModificationException — java.util.ConcurrentModificationException (Java)ConcurrentModificatio…NumberFormatException — java.lang.NumberFormatException: For input string: "x" (Java)NumberFormatExceptionOutOfMemoryError — java.lang.OutOfMemoryError: Java heap space (Java)OutOfMemoryErrorTypeError — TypeError: Cannot read properties of undefined (reading 'x') (JavaScript)TypeErrorTypeError: NoneType — TypeError: 'NoneType' object is not subscriptable (Python)TypeError: NoneTypeUnexpectedly found nil — Fatal error: Unexpectedly found nil while unwrapping an Optional value (iOS)Unexpectedly found nilNullPoint…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.