ArrayIndexOutOfBoundsException, explained and fixed

This exception is thrown when code accesses an array (or an array-backed structure reporting through the same exception) with an index that is negative or greater than or equal to its length. Modern messages are admirably complete: Index 5 out of bounds for length 5 gives the index used and the valid range, 0 to 4. The bug is never in the array; it is in how the index was computed, or in an assumption that the array would be bigger than it is.

The usual causes, most common first

  1. The classic off-by-one: a loop running with <= array.length instead of <, or starting a “human” count at 1 against a 0-based structure, so the final iteration steps one past the end.
  2. An empty or shorter array than assumed: splitting a string that has no separator returns one element, not two; a file or query returns fewer rows than the code indexes into blindly.
  3. Parallel arrays drifting apart: indexing one array with a position derived from another of a different length (names and values, keys and results).
  4. Computed indices from data: a value read from input used directly as an index without range checking, a month 13, a day 0, a category code the array was not sized for.
  5. String indexOf and substring arithmetic producing -1 or a position past the end, then reused as an array index.

The fixes, in order

  1. Read the two numbers and work out which world the index came from: if it equals the length, suspect the loop bound or the 1-based assumption first; if it is wildly larger or negative, suspect data-driven computation.
  2. Prefer iteration that cannot overrun: the enhanced for loop (for (String s : arr)) or iterating with the collection's own size removes the entire class of loop-bound errors.
  3. Validate indices that come from data at the point of computation, with a range check that produces a meaningful error (“category 13 has no slot”) instead of the raw exception.
  4. Check emptiness before indexing: guard arr[0] patterns with a length test whose branch handles the empty case as a real outcome, an empty result is usually a legitimate answer, not an impossibility.

Common questions

Why index 5 with length 5? Isn't 5 the last one? No: indices run 0 through length-1, so a length-5 array's last index is 4. An index equal to the length is the single most common form of this error, the loop bound written with <= instead of <.

I get this from a library call, not my own array code, why? Some library methods index internally based on arguments you supply (ranges, pages, sublists). The index arithmetic is still yours; check the from/to values passed in, especially end-exclusive conventions.

Does it happen with Lists too? Lists throw IndexOutOfBoundsException (the parent type, without “Array”). The causes and fixes are the same; seeing the Array variant tells you an actual array is involved somewhere in the call.

Related errors

NullPointerException · IllegalArgumentException · StackOverflowError