Java Guide: Fix ArrayOutOfBounds

How to fix/prevent the java error java.lang.ArrayIndexOutOfBoundsException and what causes it.

Problem:
I cant compile my java code, I always get the error message java.lang.ArrayIndexOutOfBoundsException. What am I doing wrong?

Answer:
You try to access the array with an illegal index, which is the cause of the compiler error. That happens when the index is negative, greater than or equal of the size of the array.

Example:
//initialize array
int[] myArray = new int[11];
// exception, 777 is greater then 11
int error = myArray[777];

So in order to avoid this, just take a closer look how you access your arrays.


More information

Java API Documentation


back