Java ProgramsExceptionsNullPointerException

NullPointerException in Java

beginner·  Exceptions  ·  Error Handling

Problem

A NullPointerException is thrown when code tries to use a reference that doesn't point to any object yet — calling a method, reading a field, or indexing through a null value.

Call a method on a String reference that hasn't been assigned an object, and handle the resulting exception.

Input
String text = null; text.length()
Output
Error: Cannot call length() on a null reference

Java Program

Java
public class NullPointerExceptionDemo { public static void main(String[] args) { String text = null; try { int length = text.length(); // text has no object to call length() on System.out.println("Length: " + length); } catch (NullPointerException e) { System.out.println("Error: Cannot call length() on a null reference"); } } }

Output

Error: Cannot call length() on a null reference

Core Logic

Wrapping the risky method call in try-catch lets the null reference be caught after the fact, instead of crashing the program.

How It Works
  1. 1String text = null; declares a reference that doesn't point to any actual String object.
  2. 2text.length() tries to invoke a method through that null reference, which the JVM cannot do.
  3. 3This throws a NullPointerException at the moment of the call, before length() ever runs.
  4. 4The catch (NullPointerException e) block catches it and prints a message instead of letting the program crash.
With text holding null, calling text.length() throws immediately, caught and printed as "Error: Cannot call length() on a null reference".
💡

Key Point: The exception is thrown at the point of use — calling .length() — not at the point where text was assigned null; declaring a null reference is perfectly legal, only using it isn't.

Key Concepts

try / catchnull referenceNullPointerException

Approach 2: Java 8

Java
import java.util.Optional; public class NullPointerExceptionOptional { public static void main(String[] args) { String text = null; // Optional.ofNullable(null) is empty, so map() is skipped and orElse() supplies the fallback String message = Optional.ofNullable(text) .map(String::length) .map(len -> "Length: " + len) .orElse("Error: Cannot call length() on a null reference"); System.out.println(message); } }

Output

Error: Cannot call length() on a null reference

Core Logic

Optional.ofNullable() wraps the possibly-null reference itself, so length() is only ever called on a value known to be present — the null case is handled by orElse() instead of a catch block.

How It Works
  1. 1Optional.ofNullable(text) wraps text, producing an empty Optional since text is null.
  2. 2.map(String::length) would call length() on the wrapped value, but only runs when the Optional is actually present — it's skipped entirely here.
  3. 3.map(len -> "Length: " + len) formats the result if text had held a real String.
  4. 4.orElse("Error: Cannot call length() on a null reference") supplies the fallback message for the empty case, without length() ever being invoked on a null reference.
With text holding null, Optional.ofNullable(text) is empty, so both map() calls are skipped and orElse(...) supplies the error message.
💡

Key Point: No NullPointerException is ever thrown in this version — Optional makes the null case part of the value's type, so it's handled by orElse() as regular data flow instead of by catching an exception after the fact.

Key Concepts

Optionalmap()orElse()

Related Programs