Null reference in AP Computer Science A

In AP Computer Science A, a null reference is a reference variable that does not point to any object. Per EK 1.14.A.2, calling an instance method on a null reference causes a NullPointerException, a runtime error, because there is no object for the dot operator to act on.

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

What is null reference?

A null reference is a reference variable whose value is null, meaning it points to no object at all. Think of a reference variable as a sticky note with an address on it. A null reference is a sticky note with no address written down. The variable exists, it has a name and a type, but there's nothing on the other end.

That's exactly why the dot operator fails on null. When you write obj.someMethod(), Java follows the reference to the object and runs the method on it. If the reference is null, there's no object to follow it to, so Java throws a NullPointerException at runtime. This is spelled out in EK 1.14.A.2: a method call on a null reference will result in a NullPointerException. Note that String s = null; compiles just fine. The crash only happens when the code actually runs and tries to use the dot operator on that null variable.

Why null reference matters in AP® Computer Science A

Null references live in Topic 1.14 (Calling a Void Method) in Unit 1: Using Objects and Methods, under learning objective 1.14.A, which asks you to call instance methods and determine the result of those calls. Sometimes "the result" is a NullPointerException, and the exam expects you to spot that. This matters beyond Unit 1, too. Any time the course hands you object references (Strings, objects stored in arrays or ArrayLists), null is lurking as a possible value. Tracing whether a reference is null before a method call is one of the most common "what does this code do?" traps in multiple-choice questions, and writing code that accidentally calls a method on null costs points on FRQs.

How null reference connects across the course

Dot Operator (Unit 1)

The dot operator is the thing that breaks on null. Per EK 1.14.A.1, you call instance methods with objectName.methodName(). If objectName is null, the dot has nothing to dereference, and Java throws a NullPointerException.

Instance Methods (Unit 1)

Instance methods run on a specific object. A null reference means there is no specific object, so the call can't happen. This is why static methods don't have this problem; they belong to the class, not an object.

Object Creation with Constructors (Unit 1)

Declaring a reference variable and creating an object are two separate steps. Robot r2; or Robot r2 = null; gives you a variable with no object behind it. Only new Robot() actually creates the object the reference can point to.

Arrays and ArrayLists of Objects (later units)

When you store object references in an array, any slot you never filled with a real object holds null. Looping over that array and calling a method on each element will crash on the null slot, which is a favorite exam setup.

Is null reference on the AP® Computer Science A exam?

Null references show up almost entirely in code-tracing multiple-choice questions, and the test is whether you can predict the outcome. Common setups include code like String word = null; int count = word.length(); where you have to identify that a NullPointerException occurs at runtime (not a compile-time error). Another classic gives you a null check, such as if (message != null), and asks what prints. The null check guards the method call, so the else branch runs and no exception is thrown. A third pattern declares two references, like Robot r1 = new Robot(); Robot r2 = null;, then calls methods on both, and you have to notice which call crashes. Harder versions hide the null inside an array of objects, where a loop calls a method on every element and one element was never initialized. On FRQs, the skill is defensive. If your method receives object references that could be null, calling a method on one without thinking can produce a runtime error in your logic.

Null reference vs Empty string ("")

An empty string is a real String object that happens to contain zero characters, so calling methods on it works fine. "".length() returns 0. A null reference is no object at all, so null.length() (via a null variable) throws a NullPointerException. The empty string has a sticky note pointing to a real (empty) box; null has no box. The MCQ answer choices often include both "prints 0" and "NullPointerException" specifically to test this distinction.

Key things to remember about null reference

  • A null reference is a reference variable that points to no object, and calling any instance method on it throws a NullPointerException (EK 1.14.A.2).

  • NullPointerException is a runtime error, not a compile-time error. The code compiles fine and only crashes when it executes the bad method call.

  • Null is not the same as an empty string. The empty string "" is a real object whose length() returns 0, while null is the absence of any object.

  • Declaring a reference variable does not create an object. You need the new keyword (or an assignment to an existing object) before you can safely use the dot operator.

  • A null check like if (obj != null) before a method call prevents the exception, and exam questions often test whether you notice the check is there.

  • When tracing code with multiple references, track which variables actually point to objects, because the exam loves mixing one real object with one null reference.

Frequently asked questions about null reference

What is a null reference in Java?

A null reference is a reference variable whose value is null, meaning it doesn't point to any object. In AP CSA, the key fact (EK 1.14.A.2) is that calling an instance method on a null reference throws a NullPointerException.

Is a NullPointerException a compile-time error?

No. Code like String s = null; s.length(); compiles with no errors. The NullPointerException only happens at runtime, when Java actually tries to follow the null reference to an object that isn't there. Picking "compile-time error" instead of "runtime exception" is one of the most common MCQ mistakes.

Is null the same as an empty string in AP Comp Sci A?

No. The empty string "" is a real String object, so "".length() returns 0 without any error. Null means there's no object at all, so calling length() on a null reference crashes with a NullPointerException.

How do you avoid a NullPointerException on the AP exam?

Check the reference before using it, like if (message != null) before calling message.toUpperCase(). Exam questions often include this exact guard, and the answer depends on whether you notice the null check routes execution to the else branch.

Does declaring a variable like Robot r2; create an object?

No. Declaring a reference variable only creates the variable, not an object. Until you assign it with new Robot() or an existing object, the reference is null (or unusable), and calling r2.move() will throw a NullPointerException.