ArrayIndexOutOfBoundsException
Java Programming Language
Severity: ModerateWhat Does This Error Mean?
An ArrayIndexOutOfBoundsException means your code tried to access a position in an array that does not exist. Arrays in Java are numbered starting from 0, not 1. So an array with 5 items has positions 0, 1, 2, 3, and 4 — position 5 does not exist. Trying to read position 5 crashes the program with this error.
Affected Models
- Java 8
- Java 11
- Java 17
- Java 21
- All Java versions
Common Causes
- Using array.length as an index — since arrays start at 0, the last valid index is array.length minus 1
- Starting a loop at index 1 instead of 0, or ending it one step too far
- Hardcoding an index number that works during development but breaks when the data changes
- Accessing an array with a negative index value — Java arrays do not support negative indexing
- An empty array with length 0 — any index access on it, including index 0, will crash
How to Fix It
-
Read the error message — it tells you the bad index and the array size. For example: 'Index 5 out of bounds for length 5' means you tried to access index 5 on an array of 5 items (valid indexes: 0 to 4).
This is always an off-by-one problem. You are either one step too far, or you are using the wrong starting point.
-
Check any loops that iterate over the array. A for loop should end with i < array.length, not i <= array.length. The <= form goes one step too far.
Correct: for (int i = 0; i < myArray.length; i++) Incorrect: for (int i = 0; i <= myArray.length; i++)
-
Use a for-each loop instead of an index loop when you just need to read all items. This eliminates index errors entirely because Java manages the iteration for you.
Example: for (String item : myArray) { ... } — no index, no out-of-bounds risk.
-
Before accessing an array with a variable index, add a bounds check: if (index >= 0 && index < array.length) before using the index.
This prevents crashes when the index comes from user input, a calculation, or an external source you do not fully control.
-
Consider using an ArrayList instead of a plain array. ArrayList has built-in methods like get(), size(), and contains() that are safer and more flexible than raw array access.
ArrayLists also grow automatically — you never need to worry about their size when adding items.
When to Call a Professional
ArrayIndexOutOfBoundsExceptions are always fixable in your code — no external help is needed. The error message includes the index that was attempted and the array size, which tells you exactly what went wrong. Focus on the loop or line the stack trace points to.
Frequently Asked Questions
Why does Java start arrays at index 0 instead of 1?
Most programming languages start at 0 because it maps directly to how computer memory works. The index is an offset from the start of the array — index 0 means 'no offset', which is the first item. It feels strange at first but becomes natural quickly.
What is the difference between ArrayIndexOutOfBoundsException and IndexOutOfBoundsException?
ArrayIndexOutOfBoundsException is for plain Java arrays. IndexOutOfBoundsException is the parent class and is also thrown by collections like ArrayList. Both mean the same thing: you used an index that does not exist in that container.
How do I safely get the last item in an array?
Use array[array.length - 1] to get the last element. First check that the array is not empty: if (array.length > 0) before accessing it. Accessing any index on an empty array will always throw this exception.