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

In this blog post, Weare going to 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 manipulation of data 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 runtime exception which occurs at during java execution in java virtual machine. Runtime exceptions might not declare in method signatures unline checked exceptions has to declare in method signature declaration
Java throws ArrayIndexOutOfBoundsException exception when an invalid index is accessed in the array, meaning when index is value is not in 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 index 0 to 9 has the default values Null(Integer object default value). Accessing array with 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  


1. _Always check for expected invalid index  in array bounders_

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

Possibility fix is to  have for loop check for valid ranges


for(int index=0;index<array.length;index++)</pre>


2. _**Fix for  Array Index Bounds Exception  as well as IndexOutOfBounds Exception for arraylist**_

ArrayList also has index based methods like set  and get This exception throws when we are accessing ArrayLlist elements with get method before  adding the elements/objects into arraylist using set method. So make sure that add below check in arraylist or vectors

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


Please click +1 if you like this post.
Join 90,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts