Repaso
Español
Java CoreFoundationsJV-53

What does null mean in Java, and what is a NullPointerException?

Options

  1. null means no object is referenced; using it throws NullPointerException (Correct option)
  2. null equals zero; the exception appears when dividing by zero
  3. null is an empty string; the exception happens on empty strings
  4. null is a keyword that marks the end of a loop

Answer

null means a reference points to no object; calling a method on it throws a NullPointerException

Why

A variable that holds an object really holds a reference (an address). null means 'no object'.

String s = null; s.length(); crashes with NullPointerException (NPE), because there is no object to ask for its length.

Typical fixes: check for null, initialize variables, or use Optional to make 'maybe nothing' explicit.

Remember

null = no object. Using it as an object → NPE.

Key terms

Practice this question in Repaso