PlainError

java.lang.NullPointerException

Last checked 2026-11-11 · 3 fixes · reviewed answer

TL;DRThis is a common Java error that happens when code tries to use an object that does not exist yet (it is null). The program usually crashes at that point.

Most likely cause: A variable expected to hold an object is empty when the code tries to call a method or access a field on it.

Try first: Locate the stack trace

Likely causes

  1. A variable expected to hold an object is empty when the code tries to call a method or access a field on it.most likely
  2. Missing initialization of an object before it is used in a method or class.possible
  3. An external library or API returned null unexpectedly and the code did not check for it.less likely

How to fix it

Safest first: SafeChanges settingsDangerous
  1. Locate the stack traceSafe

    Find the full error output or log file; it lists the exact line number and file where the null value was used.

  2. Check the line mentioned in the traceSafe

    Look at the code on that line and trace back any variables that could be null at that moment.

  3. Add a null check before the failing lineChanges settings

    Wrap the problematic code with an if statement that tests whether the object is null and handles that case safely.

If that didn't fix it

Add debug print statements right before the crashing line to print the values of the variables involved.

When to see a technician

Ask the developer who wrote or maintains the Java code if the stack trace does not point to a line you can edit.

Quick answers

What does java.lang.NullPointerException mean?
This is a common Java error that happens when code tries to use an object that does not exist yet (it is null). The program usually crashes at that point.
What causes java.lang.NullPointerException?
A variable expected to hold an object is empty when the code tries to call a method or access a field on it. (most likely) Missing initialization of an object before it is used in a method or class. (possible) An external library or API returned null unexpectedly and the code did not check for it. (less likely)
How do I fix java.lang.NullPointerException?
Try these in order, safest first: 1. Locate the stack trace. 2. Check the line mentioned in the trace. 3. Add a null check before the failing line.
What if that didn't fix it?
Add debug print statements right before the crashing line to print the values of the variables involved.
When should I see a technician?
Ask the developer who wrote or maintains the Java code if the stack trace does not point to a line you can edit.
Did this fix it? Report a problem

Related errors

All Coding errors →