Null

In AP Computer Science A, null is the special Java value that means a reference variable does not refer to any object. Calling a method or accessing data through a null reference throws a NullPointerException, one of the most commonly tested runtime errors on the exam.

Verified for the 2027 AP Computer Science A examLast updated June 2026

What is null?

Null is Java's way of saying "this variable exists, but it isn't pointing at anything yet." Reference variables (like String, Rectangle, or ArrayList variables) don't hold objects directly. They hold a reference, basically an arrow pointing to an object in memory. When that arrow points at nothing, the variable's value is null.

You'll meet null in a few predictable places. Instance variables of a reference type (like a String field in a class) automatically default to null if a constructor never assigns them. A variable you declare without using the new keyword starts as null too. The danger comes when you try to use a null reference, like calling playerName.length() when playerName was never assigned. Java can't call a method on nothing, so it throws a NullPointerException and your program crashes. A huge chunk of debugging in AP CSA comes down to tracking which references are null and when.

Why null matters in AP Computer Science A

Null sits at the heart of how Java handles objects, which is the backbone of Units 2 and 5 (Using Objects and Writing Classes). The CED expects you to know default values, and they split by type. Primitive instance variables like int default to 0 and double to 0.0, but reference-type instance variables default to null. That distinction shows up constantly in MCQs that ask about an object's state right after construction. Null also matters for safe code. Before calling a method on an object that might not exist, you check if (obj != null) first. Skipping that check is exactly how a NullPointerException happens, and recognizing when code will throw one is a classic exam skill.

How null connects across the course

Default Value (Unit 5)

Null IS the default value for reference-type instance variables. If a constructor sets length and width but never touches a String name field, that field is null automatically. Exam questions love asking what an object's state is right after a constructor runs, and the answer often includes a null somewhere.

New Keyword (Unit 2)

The new keyword is the opposite of null. Writing Rectangle r; gives you a null reference, while Rectangle r = new Rectangle(3, 4); actually creates an object for r to point to. If you forget new, every method call on that variable throws a NullPointerException.

Empty String (Unit 2)

A null String and an empty String ("") are completely different. An empty String is a real object with length 0, so you can call methods on it safely. A null String is no object at all, so s.length() crashes. This is one of the most commonly tested distinctions in early CSA units.

ArrayIndexOutOfBoundsException (Unit 6)

NullPointerException and ArrayIndexOutOfBoundsException are the two runtime errors you're most likely to see in MCQ answer choices. One means the reference points at nothing, the other means the array exists but you reached past its edge. Knowing which error a code segment produces is a frequent exam ask.

Is null on the AP Computer Science A exam?

Null is tested in two main ways. On the multiple-choice section, expect code segments where you have to predict whether a NullPointerException occurs, like a constructor that never initializes a reference field, or a method called on a variable that was declared but never assigned with new. Questions about an object's state right after construction also hinge on knowing that reference fields default to null while primitives default to 0 or 0.0. On the FRQs, null shows up as a defensive-coding habit. Class design and ArrayList questions (like the 2017 free-response set built around classes such as Digits and Phrase) reward you for initializing instance variables in constructors so your references are never accidentally null, and for checking != null before using objects that might not exist. You won't write an essay about null, but you will lose points if your code calls methods on references you never initialized.

Null vs Empty String

An empty String ("") is a real String object that happens to contain zero characters, so s.length() returns 0 and s.equals("") returns true. Null means there is no String object at all, so calling any method on it throws a NullPointerException. Think of it like a mailbox. An empty String is an empty mailbox, while null means the mailbox was never installed.

Key things to remember about null

  • Null means a reference variable points to no object at all, not that it holds zero or an empty value.

  • Calling any method or accessing any field through a null reference throws a NullPointerException at runtime.

  • Reference-type instance variables default to null if a constructor never assigns them, while primitive instance variables default to 0, 0.0, or false.

  • A null String and an empty String are different. The empty String is a real object with length 0, so methods work on it safely.

  • Using the new keyword is what replaces null with an actual object, so a declared-but-never-constructed variable stays null.

  • Checking if (obj != null) before using an object is the standard way to avoid NullPointerExceptions in FRQ code.

Frequently asked questions about null

What is null in AP Computer Science A?

Null is the special Java value meaning a reference variable doesn't point to any object. It's the automatic starting value for reference-type instance variables like String or Rectangle fields that a constructor never initializes.

Is null the same as 0 or an empty string in Java?

No. Zero is a primitive int value and "" is a real String object with zero characters, but null means there's no object at all. You can call methods on an empty String safely, while calling any method on null crashes with a NullPointerException.

What causes a NullPointerException on the AP CSA exam?

It happens when code calls a method or accesses a field through a reference that is null, like calling name.length() when name was declared but never assigned with new or in a constructor. Spotting this in code segments is a classic multiple-choice question.

Do int and double variables ever equal null?

No. Primitives like int, double, and boolean can never be null because they hold actual values, not references. Only reference types (String, arrays, ArrayList, and any class you write) can be null. As instance variables, primitives default to 0, 0.0, or false instead.

How do I avoid NullPointerExceptions in my FRQ code?

Initialize every reference-type instance variable in your constructors, always use new before calling methods on an object, and check if (obj != null) when an object might not exist. These habits keep your FRQ solutions from losing points to runtime errors.