What does ArrayIndexOutOfBoundsException mean and how do I get rid of it?

Here is a code sample that triggers the exception:

String[] names = { "tom", "bob", "harry" };
for (int i = 0; i <= names.length; i++) {
    System.out.println(names[i]);
}
java arrays exception 

Nov 17

4 Answers

Your first port of call should be the documentation that explains it reasonably clearly:

Thrown to indicate that an array has been accessed with an illegal index. The index is either negative or greater than or equal to the size of the array.

So for example:

int[] array = new int[5];
int boom = array[10]; // Throws the exception

As for how to avoid it... um, don't do that. Be careful with your array indexes.

One problem people sometimes run into is thinking that arrays are 1-indexed, e.g.

int[] array = new int[5];
// ... populate the array here ...
for (int index = 1; index <= array.length; index++)
{
    System.out.println(array[index]);
}

That will miss out the first element (index 0) and throw an exception when index is 5. The valid indexes here are 0-4 inclusive. The correct, idiomatic for statement here would be:

for (int index = 0; index < array.length; index++)

(That's assuming you need the index, of course. If you can use the enhanced for loop instead, do so.)

answered Jan 10


In order to avoid the java.lang.ArrayIndexOutOfBoundsException, you should always do the bound check before accessing array element e.g.

if (args.length &lt; 2) {
  	System.err.println("Not enough arguments received.");
  	return;
}

Always remember that the array index starts at 0 and not 1 and an empty array has no element in it. So accessing the first element will give you the java.lang.ArrayIndexOutOfBoundsException : 0 error in Java. 

You should always pay to one-off errors while looping over an array in Java. The programmer often makes mistakes that result in either missing the first or last element of the array by messing 1st element or finishing just before the last element by incorrectly using the <, >, >= or <= operator in for loops. 

For example, the following program will never print the last element of the array. The worst part is there won't be any compile-time error or runtime exception. It's a pure logical error that will cause incorrect calculation. 

/**
 * Java Program to demonstrate one-off error while looping over array. 
 * 
 * @author WINDOWS 8
 *
 */
public class HelloWorldApp {

    public static void main(String args[]) {
      
       int[] primes = {2, 3, 5, 7, 11, 13, 17};
       
       for(int i = primes.length - 1; i > 0 ; i--){
           System.out.println(primes[i]);
       }

    }
}

answered Jan 10


How to Fix SQLServerException: The index 58 is out...,3 ways to create random numbers in a range in Java...,General Guide to solve java.lang.ClassNotFoundException in Java [guide], array - binary search

public class Test {

   public static void main(String[] args) {

      int[] array = new int[0];

      // bad code
      int number = array[0];

      // good code
      if (array.length > 0) {
         number = array[0];
      }
   }

}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Test.main(Main.java: 15)

public class ABC {

   public static void main(String[] args) {

      int[] array = new int[10];
      int sum = 0;

      // bad code
      for (int i = 0; i <= array.length; i++) {
         sum = sum + array[i];
      }

      // good code
      for (int i = 0; i < array.length; i++) {
         sum = sum + array[i];
      }
   }

}

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at ABC.main(Main.java: 16)

answered Jan 10


ArrayIndexOutOfBoundsException whenever this exception is coming it mean you are trying to use an index of array which is out of its bounds or in lay man terms you are requesting more than than you have initialised.

To prevent this always make sure that you are not requesting a index which is not present in array i.e. if array length is 10 then your index must range between 0 to 9

answered Jan 10


Login and Submit Your Answer
Browse other questions tagged  java  arrays  exception or ask your own question.