ArrayIndexOutOfBoundsException in Java — What It Means and How to Fix It
What Java's ArrayIndexOutOfBoundsException means and how to fix it.
Published November 2, 2026
ArrayIndexOutOfBoundsException is thrown when you access an array index that is negative or not less than the array's length.
What it means and why it happens
Typical causes: off-by-one loops (<= instead of <), empty arrays or wrong index calculations.
Read the full message: it names the file and line where the problem was detected, which is the best place to start.
What you see
run the code
java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 3
Practical examples
Code that triggers it
int[] a = new int[3];
a[3] = 1;The fix
int[] a = new int[3];
for (int i = 0; i < a.length; i++) a[i] = i;Safe access
if (index >= 0 && index < a.length) { use(a[index]); }Common mistakes
- Using <= a.length in loops.
- Assuming arrays resize.
- Indexing before checking length.
Key takeaways
- ArrayIndexOutOfBoundsException: ArrayIndexOutOfBoundsException is thrown when you access an array index that is negative or not less than the array's length.
- Read the message and the line number first.
- Fix the cause, don't just silence the error.
FAQ
Array vs list?
Lists throw IndexOutOfBoundsException; arrays throw ArrayIndexOutOfBoundsException.
How do I find which line caused ArrayIndexOutOfBoundsException?
The error message and stack trace show the file and line; start there and work backwards.