2 Ways to solve java.lang.ArrayIndexOutOfBoundsException errors in java

In this blog post, learn How to Fix/handle java.lang.ArrayIndexOutOfBoundsException errors in java

What is java.lang.ArrayIndexOutOfBoundsException error in java?

This exception is one of the common exceptions in java.

Java developers used to get this exception in the projects when data was retrieved from the database as well as dealing with arrays and array lists.

ArrayIndexOutOfBoundsException is a class in java. lang package which extends IndexOutOfBoundsException, extended by RuntimeException. IndexOutOfBoundsException is a runtime exception which occurs during java execution in a java virtual machine. Runtime exceptions might not declare in method signatures unline checked exceptions has to declare in the method signature declaration Java throws an ArrayIndexOutOfBoundsException exception when an invalid index is accessed in the array, meaning when the index value is not in the range between zero and array.length-1. And the sample exception print stack trace is as follows.

Exception in thread “main” java.lang.ArrayIndexOutOfBoundsException: -1

Below java program code throws ArrayIndexOutOfBoundsException exception

public class ArrayIndexOutOfBoundsExceptionExample {

public static void main(String\[\] args) {
 Integer array\[\] = new Integer\[10\];
 System.out.println(" array index 0  value " + array\[0\]);
 System.out.println(" array index 10 value " + array\[10\]);
 }
}

Above java code create an Integer array of size 10, In Java, an array‘s index always start with 0 and the last index is 9 Array with an index of 0 to 9 has the default values Null(Integer object default value). Accessing an array with an index out of this range (0 -9) throws this exception. Executing array[0] executes fine and outputs null, whereas array[10] is invalid and index 10 is invalid, so the java program throws Array Index Exception.

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
 at ArrayIndexOutOfBoundsExceptionExample.main(ArrayIndexOutOfBoundsExceptionExample.java:6)

How to handle ArrayIndexOutOfBoundsException error in java?

Let us see how to handle/ solve IndexOutOfBoundsException exception types in java

  • Always check for the expected invalid index in array bounders

Arrays are fixed in size and always start with an index of 0. We have to write a conditional check to consider the array elements between 0 to n-1 if the array size is n-1

A possibility fix is to have for loop check for valid ranges.


for(int index=0;index<array.length;index++)
  • Fix for Array Index Bounds Exception as well as IndexOutOfBounds Exception for ArrayList

ArrayList also has index-based methods like set and get. The Exception throws when ArrayList accesses elements with the get method before adding the elements/objects into the ArrayList using the set method. So make sure that add the below check-in ArrayList or vectors.

if(index >0 || index <= arraylist.size())

Please click +1 if you like this post.