An ArrayIndexOutOfBoundsException is thrown when... The Array index is out of bounds.
An example of this
Code:
String[] array= new String[] {"x", "y", "z"};
System.out.println("Here is where exception will be caught" + array[4]);
This gives you an exception because the indices of the array only go up to 2. If you're wondering why its 2 and not 3 it's because array indices are zero based so doing this
Would also throw an exception.
How to fix:
This depends on the situation but usually just extending the array would suffice.
Code:
String[] array = new String[] {"x", "y", "z", "1", "2", "3"};
Would fix the exception I just provided you with. But sometimes extending the array won't actually fix the problem. This is because at times whatever is calling the array is calling the wrong index/indices. Then what you would have to do is make sure it's not calling the wrong index.
So say you're trying to call for "z". Doing this
Code:
String[] array = new String[] {"x", "y", "z"};
System.out.println("Here is where exception will be caught" + array[4]);
Would throw an exception and if you try extending it then obviously the problem wont be fixed. Instead of extending , this time you're going to have to call the right index so
Code:
System.out.println("This will print out the letter z: " + array[2]);