Why does ArrayIndexOutOfBoundsException occur and how to avoid it in Android? [closed]

This exception is thrown when you try to access an array item that doesn’t exist:

String [] myArray = new String[2];

myArray[2] = "something"; // Throws exception
myArray[-1] = "something"; // Throws exception

You should check that your index is not negative and not higher than the array length before accessing an array item.

Leave a Comment