ArrayIndexOutOfBoundsException in AP Computer Science A
In AP Computer Science A, an ArrayIndexOutOfBoundsException is a runtime error Java throws when code accesses an array with an index that is negative or greater than or equal to the array's length. For a 1D array, the only valid indexes run from 0 to arr.length - 1.
What is ArrayIndexOutOfBoundsException?
An ArrayIndexOutOfBoundsException is what Java throws when you reach for an array slot that doesn't exist. Arrays are zero-indexed, so an array with length 5 has slots 0, 1, 2, 3, and 4. Ask for arr[5] or arr[-1] and the program crashes with this exception. Index 5 is the classic mistake, because it feels like it should be the fifth element.
The exception exists because of a rule in the CED. EK 4.3.A.2 says an array's length is set when the array is created and can't be changed. The array has a fixed number of boxes, and Java checks every access against that number while the program runs. 2D arrays follow the same logic. EK 4.11.A.1 describes a 2D array as an array of arrays, so each bracket gets its own check. In matrix[r][c], r must be between 0 and matrix.length - 1, and c must be between 0 and matrix[r].length - 1. If either index falls outside its range, you get the exception.
Why ArrayIndexOutOfBoundsException matters in AP® Computer Science A
This term lives in Unit 4: Data Collections, specifically Topic 4.3 (Array Creation and Access) and Topic 4.11 (2D Arrays). It supports two learning objectives. AP Comp Sci A 4.3.A asks you to develop code that represents collections of related data using 1D arrays. AP Comp Sci A 4.11.A asks the same for 2D arrays. You can't write correct array code without knowing where the edges are, and this exception is how Java tells you that you went past one.
On the exam, the term is really a test of whether you understand zero-based indexing and the length attribute. Many tricky multiple-choice questions come down to one character, < versus <= in a loop header. The exception is the consequence of getting that character wrong.
Keep studying AP® Computer Science A Unit 4
How ArrayIndexOutOfBoundsException connects across the course
Index and the length attribute (Unit 4)
This is the closest link. The length tells you how many boxes the array has, and the last valid index is always one less than that number. The exception is just Java enforcing the gap between "how many" and "which one."
2D arrays, row and column index (Unit 4)
A 2D array is an array of arrays, so you get two separate chances to go out of bounds. matrix.length counts rows and matrix[0].length counts columns. If you mix them up on a non-square grid, you'll walk right off the edge.
Loop traversals and off-by-one errors (Unit 4)
Most of these exceptions come from a loop that runs one step too far, like for (int i = 0; i <= arr.length; i++). The last pass asks for arr[arr.length], a slot that was never created. Using < instead of <= fixes it.
Default values and object references (Unit 4)
Under EK 4.3.A.3, every slot inside the bounds exists from the moment you call new. Those slots hold 0, 0.0, false, or null. Reading an in-bounds slot you never assigned gives you the default value, not an exception. Only indexes outside the array crash the program.
Is ArrayIndexOutOfBoundsException on the AP® Computer Science A exam?
The exam doesn't ask you to recite a definition. It asks you to trace code and spot where it would break. Expect these kinds of questions:
- "What is printed?" tracing. A question might build
int[] numbers = new int[5];, fill indexes 0, 2, and 4, and then printnumbers[1]andnumbers[3]. Those indexes are in bounds, so the output is0 0from the default values, not an error. One answer choice will usually be the exception, waiting to catch you if you mix up "unassigned" with "out of bounds." - 2D index lookup. Given
int[][] matrix = {{10, 20, 30}, {40, 50, 60}, {70, 80, 90}};, the value 40 sits atmatrix[1][0]. A wrong choice likematrix[2][1]comes from counting from 1, the same habit that causes out-of-bounds crashes. - Building arrays with offset formulas. In a multiplication table where
[i][j]holds(i+1)*(j+1), the+1belongs in the value, not in the index. Loop indexes still have to run from 0 to length - 1. - Parallel arrays. A method that loops
nameswithi < names.lengthwhile also readingscores[i]only works if both arrays have the same length. Ifscoreswere shorter, the code would throw the exception.
No released FRQ has used this term verbatim. Even so, every array-based FRQ quietly tests it, because a loop bound that runs one step too far costs you points for incorrect code.
ArrayIndexOutOfBoundsException vs NullPointerException
Both are runtime errors, but they come from different problems. An ArrayIndexOutOfBoundsException means the slot doesn't exist, because the index is below 0 or at least the length. A NullPointerException means the slot exists but holds null (the default for reference types under EK 4.3.A.3), and you tried to call a method on it. For example, String[] words = new String[3]; followed by words[3].length() is out of bounds. Writing words[0].length() instead is a null pointer problem, because slot 0 exists but is empty.
Key things to remember about ArrayIndexOutOfBoundsException
An ArrayIndexOutOfBoundsException is thrown at runtime when an index is negative or greater than or equal to the array's length.
Valid indexes for a 1D array always run from 0 to arr.length - 1, so arr[arr.length] is never legal.
In a 2D array, the row index must stay below matrix.length and the column index must stay below matrix[row].length.
Loop headers that use <= arr.length instead of < arr.length are the most common cause of this exception on the exam.
Reading an in-bounds element you never assigned returns its default value (0, 0.0, false, or null) and does not throw this exception.
Because array length is fixed at creation, you cannot avoid this exception by "growing" an array. You have to keep your indexes inside the original bounds.
Frequently asked questions about ArrayIndexOutOfBoundsException
What is an ArrayIndexOutOfBoundsException in AP CSA?
It's the runtime error Java throws when you access an array with an index less than 0 or greater than or equal to its length. For an array of length 5, only indexes 0 through 4 are valid, so arr[5] throws it.
Is ArrayIndexOutOfBoundsException a compile-time error?
No. The code compiles fine, and the exception only happens when the program runs and actually tries the bad index. That's why it counts as a runtime error, and you have to trace the code to catch it.
Does accessing an unassigned array element throw ArrayIndexOutOfBoundsException?
No, as long as the index is in bounds. After int[] numbers = new int[5];, numbers[3] returns the default value 0 even if you never assigned it. Only indexes outside 0 to 4 would throw the exception.
How is ArrayIndexOutOfBoundsException different from NullPointerException?
ArrayIndexOutOfBoundsException means you asked for a slot that doesn't exist. NullPointerException means the slot exists but holds null, and you tried to use it like an object. In a new String[3], words[3] is out of bounds, while calling words[0].length() is a null pointer error.
How do you avoid ArrayIndexOutOfBoundsException in 2D arrays?
Loop rows with r < matrix.length and columns with c < matrix[r].length (or matrix[0].length for rectangular arrays). Don't swap rows and columns on a non-square grid. A 3-by-4 array has rows 0 to 2 and columns 0 to 3.
Keep studying AP Computer Science A
Connect this key term to the AP exam workflow: review the course, practice questions, and check related study tools.
AP Computer Science A hub
Review units, study guides, and course resources.
AP-style practice
Check this vocabulary in multiple-choice context.
FRQ practice
Apply key concepts in written AP responses.
Score calculator
Estimate the exam score you are working toward.
Cheatsheets
Review the highest-yield facts before practice.
Practice exam
Put the full course together before test day.