Runtime Errors in AP CSA: NPE and AIOOB Complete Guide (2025-2026)

Runtime Errors in AP CSA: NPE and AIOOB Complete Guide (2025-2026)

Runtime errors in AP CSA — specifically NullPointerException (NPE) and ArrayIndexOutOfBoundsException (AIOOB) — are among the most frequently tested error types on the AP Computer Science A exam. Unlike compile-time errors, runtime errors happen during program execution when something unexpected occurs. The AP exam presents code that compiles cleanly, runs correctly for some inputs, and then throws one of these exceptions for a specific input — and you must predict exactly what happens and why.

💻 Code Examples — Predict First

Before running each example, write down your prediction. This is the single most effective AP exam study technique.

🤔 Predict the output before running:

Example 1: NullPointerException
public class Main {
    public static void main(String[] args) {
        String s = null;
        // This compiles fine but crashes at runtime
        System.out.println(s.length());
    }
}
Running…

NullPointerException — s is null. Calling s.length() on a null reference throws NPE. The fix: check s != null before calling any method on it.

🤔 Predict the output before running:

Example 2: ArrayIndexOutOfBoundsException
public class Main {
    public static void main(String[] args) {
        int[] arr = {10, 20, 30};
        // arr.length = 3, valid indices: 0, 1, 2
        for (int i = 0; i <= arr.length; i++) {
            System.out.println(arr[i]);
        }
    }
}
Running…

Prints 10, 20, 30 then throws AIOOB — arr.length=3. Valid indices are 0,1,2. When i=3, arr[3] is out of bounds. Fix: use i < arr.length (strict less-than).

🤔 Predict the output before running:

Example 3: Correct Max Finder (No Runtime Error)
public class Main {
    public static void main(String[] args) {
        int[] arr = {5, 3, 8, 1};
        int max = arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i] > max) {
                max = arr[i];
            }
        }
        System.out.println(max);
    }
}
Running…

8 — This is the CORRECT pattern. max initializes to arr[0], loop starts at i=1, uses i < arr.length. No AIOOB possible. Study this as the reference implementation.

❌ Common Pitfalls

These are the mistakes students most often make on the AP CSA exam with runtime errors AP CSA NullPointerException. Study them carefully.

1
⚠ Using <= arr.length instead of < arr.length

This is the #1 cause of AIOOB on the AP exam. Valid array indices are 0 to length-1. The index equal to length is always out of bounds.

for (int i=0; i<=arr.length; i++)  // CRASHES at i=length
for (int i=0; i
  
2
⚠ Calling a method on a null reference

Any method call on a variable that holds null throws NPE. This includes .length(), .equals(), .charAt(), and any other method. Always null-check before calling methods on reference variables.

String s = null;
s.length(); // NPE
if (s != null) s.length(); // Safe
3
⚠ Accessing arr[-1] or other negative indices

Negative array indices always throw AIOOB. This can happen in reverse loops when the decrement goes below 0, or when computing an index from a formula that produces a negative result.

int[] arr = {1,2,3};
arr[-1]; // AIOOB: index -1 is invalid
4
⚠ Confusing compile-time and runtime errors

NPE and AIOOB compile perfectly. The compiler cannot detect them because the problematic values (null, out-of-range index) are only known at runtime. On the AP exam, questions about these errors always present syntactically valid code.

int[] arr = new int[3];
arr[5] = 10; // Compiles! Crashes at runtime.
🎓 AP Exam Tip

On the AP CSA exam, when a question shows code and asks 'What is the result?', one of the choices is always an exception. If you see a loop with <= arr.length, automatic AIOOB. If you see a null variable with a method call, automatic NPE. Train yourself to spot these patterns instantly.

⚠ Watch Out!

The AP exam distinguishes between compile-time errors (syntax mistakes the compiler catches) and runtime errors (exceptions during execution). NPE and AIOOB are ALWAYS runtime errors — never compile-time. If a question asks which type of error, the answer is runtime (not compile-time, not logic error).

✍ Check for Understanding (8 Questions)

Your Score: 0 / 0
1. What type of error is a NullPointerException?
2. What is printed before the program crashes?
int[] arr={1,2,3};
for(int i=0;i<=arr.length;i++)
 System.out.print(arr[i]+" ");
3. Which line causes a NullPointerException?
String a = "hello";
String b = null;
System.out.println(a.length());
System.out.println(b.length());
4. What is the maximum valid index for an array declared as int[] arr = new int[7]?
5. Which of the following PREVENTS an ArrayIndexOutOfBoundsException?
6. This code compiles. What happens at runtime with input arr={}?
public int first(int[] arr){ return arr[0]; }
7. Examine statements I and II. Which cause(s) a runtime error?
I. String s=null; s.toUpperCase();
II. int[] a=new int[3]; a[3]=5;
8. Which is the BEST description of the difference between a compile-time error and a runtime error?

❓ Frequently Asked Questions

What is a NullPointerException in Java?

A NullPointerException (NPE) occurs when you call a method or access a field on a variable that holds null (no object). It is a runtime error that compiles successfully. Common causes: calling .length() on a null String, or calling any method on an uninitialized object reference.

What is an ArrayIndexOutOfBoundsException?

An ArrayIndexOutOfBoundsException (AIOOB) occurs when you access an array with an invalid index. Valid indices are 0 to length-1. Accessing index -1 or index equal to length always throws AIOOB. It is a runtime error.

How do I prevent NullPointerException in AP CSA?

Before calling any method on a reference variable, check if it is null: if (obj != null) obj.method(). Using short-circuit evaluation (obj != null && obj.method()) is also safe and idiomatic.

How do I prevent ArrayIndexOutOfBoundsException?

Always use strict less-than in array loops: i < arr.length (not i <= arr.length). Before accessing arr[0], check arr.length > 0. For computed indices, verify they are in the range [0, arr.length-1].

Are NPE and AIOOB compile errors or runtime errors?

Both are runtime errors. They occur during program execution, not during compilation. The code compiles successfully because the compiler cannot know at compile time what value a variable will hold or what an index will be.

TC

Tanner Crow — AP CS Teacher & Tutor

11+ years teaching AP Computer Science at Blue Valley North High School (Overland Park, KS). Verified Wyzant tutor with 1,845+ hours, 451+ five-star reviews, and a 5.0 rating. His AP CSA students score 5s at more than double the national rate.

  • 54.5% of students score 5 on AP CSA (national avg: 25.5%)
  • 1,845+ verified tutoring hours • 5.0 rating
  • Free 1-on-1 tutoring inquiry: Wyzant Profile

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

tanner@apcsexamprep.com

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at tanner@apcsexamprep.com