ArrayIndexOutOfBoundsException

In this tutorial, we will learn how to fix ArrayIndexOutOfBoundsException. This Exception occurs when an array has been accessed with an index that is negative or more than or equal to the size of array itself.

What is java.lang.ArrayIndexOutOfBoundsException?

java.lang.ArrayIndexOutOfBoundsException occurs when we try to access an element of an array, with an index that is negative or more than the size of array itself. Usually, one would come across “java.lang.ArrayIndexOutOfBoundsException: 4” which occurs when an attempt is made to access (4+1)fifth element of an array, despite the size of array being less than five.

Following is an example program that could produce this exception.

ArrayIndexOutOfBoundsExceptionExceptionExample.java