Using a hard-coded or randomly provided value as the index to an array access can cause that array access to throw an ArrayIndexOutOfBoundsException if the value is outside the bounds of that array.

This problem occurs when a literal value, or a value generated using the Random, is used as an index for an array access operation. If one or more of the range of values produced by the random operation, or the fixed value of the literal, is outside the bounds of the array then this can cause an ArrayIndexOutOfBoundsException.

If the index is a literal value, then the literal value may need to be modified to specify an index that is guaranteed to lie within the bounds of the array. Alternatively, the literal value may represent a default value that was never intended to be used in the array access, in which case the logic should be modified to ensure the default value is never used.

For indices that flow from randomly generated values, either the random operation should be modified to generate a value that is guaranteed to be within the bounds of the array, or the array access should be protected by suitable conditional checks that verify the index is smaller than the length and larger than or equal to zero.

The following program searches through an array to find the index at which some search text matches:

If the search text is not found, foundProductID is set to the default value - specified as -1. In the first access, foundProductID is used without checking whether the index is -1. This code can therefore throw a ArrayIndexOutOfBoundsException if the search text is not found.

In the second case, the array access is protected by a conditional that verifies the index is greater than or equal to zero. This ensures that an ArrayIndexOutOfBoundsException cannot be thrown.

  • Java API Specification: ArrayIndexOutOfBoundsException.