Java ArrayIndexOutOfBoundsException — Array index is out of valid range
ArrayIndexOutOfBoundsException: index out of range
Verified against OpenJDK 21 documentation, JLS §10.4, Java SE API docs · Updated June 2026
> quick_fix
Check the array length before accessing by index: use `array.length` as the upper bound in loops, and ensure index >= 0 and index < array.length before any direct access.
// Safe loop — always use < array.length, not <=
for (int i = 0; i < array.length; i++) {
System.out.println(array[i]);
}
// Safe direct access
if (index >= 0 && index < array.length) {
System.out.println(array[index]);
}What causes this error
ArrayIndexOutOfBoundsException is thrown when you try to access an array element using an index that is either negative or greater than or equal to the array's length. Java arrays are zero-indexed, so a 5-element array has valid indices 0 through 4. Accessing index 5 throws this exception.
How to fix it
- 01
step 1
Read the exception message for the index value
The message says: 'Index X out of bounds for length Y'. Identify where index X is computed in your code and why it exceeds Y-1.
- 02
step 2
Check for off-by-one in loop bounds
The most common cause is using `i <= array.length` instead of `i < array.length`. A 5-element array: valid indices are 0-4, so the loop condition must be `i < 5` (not `i <= 5`).
- 03
step 3
Validate index before use
When the index comes from external input, user data, or a calculation, validate it: `if (index < 0 || index >= array.length) throw new IllegalArgumentException("Index out of range: " + index);`
- 04
step 4
Consider using ArrayList instead of arrays
java.util.ArrayList throws IndexOutOfBoundsException with a clearer message, and its size() method always reflects the actual number of elements. For dynamic collections, prefer List over raw arrays.
- 05
step 5
Check multi-dimensional array access
For 2D arrays, ensure both dimensions are checked: `matrix[row][col]` requires `row < matrix.length` AND `col < matrix[row].length`. In jagged arrays, each row can have a different length.
How to verify the fix
- Add a debug print: `System.out.println("array.length=" + array.length + " index=" + index)` just before the access
- Run unit tests that exercise boundary conditions: first element (index 0), last element (length-1), empty array
- Remove the debug print once confirmed
Why ArrayIndexOutOfBoundsException happens at the runtime level
Java arrays are fixed-size objects allocated on the heap with a known length stored in a header field. At runtime the JVM's bytecode verifier and `aaload`/`iaload` instructions perform a bounds check on every array access: if the index is negative or >= array.length, the JVM throws ArrayIndexOutOfBoundsException. This bounds checking is mandatory and cannot be disabled in standard Java — it is a core safety guarantee that prevents buffer overflow exploits common in C/C++.
Common debug mistakes for ArrayIndexOutOfBoundsException
- Using `i <= array.length` in a for-loop condition instead of `i < array.length`
- Hardcoding the array length as a magic number that becomes stale when the array is resized
- Reading array length before all elements have been populated (array is allocated but partially initialized)
- Using the wrong dimension variable in nested loops over 2D arrays (swapping row and column)
When ArrayIndexOutOfBoundsException signals a deeper problem
ArrayIndexOutOfBoundsException almost always points to a logic error in index arithmetic rather than a truly surprising input. When it appears frequently in production, it is a signal that the code is doing manual array management in places where a higher-level data structure (ArrayList, LinkedList, or a stream) would be more appropriate. Prefer the Collections API for any dynamic-length data to eliminate this entire class of error.
Editor's take
ArrayIndexOutOfBoundsException is Java's runtime bounds check firing — the JVM verifies every array access and throws this when the index is negative or greater than or equal to the array length. Unlike C where out-of-bounds access is undefined behavior that can corrupt memory silently, Java fails immediately with the exact index that violated the bounds. This design decision prevents buffer overflow exploits but means your code must handle boundary conditions correctly. The most common production scenario is processing command-line arguments with `args[0]` without checking `args.length` first, or iterating with `for (int i = 0; i <= array.length; i++)` where the `<=` should be `<`. In multi-threaded Java applications, this exception often reveals a concurrency bug: one thread modifies an array's backing data while another reads by index. The fix isn't adding bounds checks — it's using `CopyOnWriteArrayList` or proper synchronization. Modern Java code should prefer enhanced for-loops (`for (var item : array)`) and Streams over manual index iteration whenever possible. If you're working with 2D arrays, remember that Java uses arrays-of-arrays, not rectangular arrays — each row can have a different length, making `array[i][j]` bounds-sensitive on both dimensions independently.
By Bikram Nath · Curator · Updated June 2026
Frequently asked questions
Why does Java use zero-based indexing?
Zero-based indexing is inherited from C, where an array name is a pointer to the first element and the index is an offset from that pointer. Index 0 = zero offset = first element. Java adopted this convention for consistency with C-family languages.
Is ArrayIndexOutOfBoundsException checked or unchecked?
It is unchecked (extends RuntimeException via IndexOutOfBoundsException). You should not catch it to handle normal control flow — instead fix the logic so the index is always valid.
What is the difference between ArrayIndexOutOfBoundsException and StringIndexOutOfBoundsException?
ArrayIndexOutOfBoundsException applies to array access. StringIndexOutOfBoundsException is thrown by String.charAt() and similar methods when the character index is out of the string's range. Both extend IndexOutOfBoundsException.