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.
📄 Table of Contents
💻 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:
public class Main {
public static void main(String[] args) {
String s = null;
// This compiles fine but crashes at runtime
System.out.println(s.length());
}
}
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:
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]);
}
}
}
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:
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);
}
}
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.
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
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
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
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.
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.
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)
int[] arr={1,2,3};
for(int i=0;i<=arr.length;i++)
System.out.print(arr[i]+" ");
String a = "hello";
String b = null;
System.out.println(a.length());
System.out.println(b.length());
int[] arr = new int[7]?
public int first(int[] arr){ return arr[0]; }
I.
String s=null; s.toUpperCase();II.
int[] a=new int[3]; a[3]=5;
❓ Frequently Asked Questions
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.
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.
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.
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].
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.
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
🔗 Related Topics
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
tanner@apcsexamprep.com
Courses
AP CSA, CSP, & Cybersecurity
Response Time
Within 24 hours
Prefer email? Reach me directly at tanner@apcsexamprep.com