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

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 which gets the data 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 array.
And the sample exception print stack trace is as follows

Print Stack trace for ArrayIndexOutOfBoundsException error in java?

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 a Integer array of size 10,
In java ,array ‘s index always start with 0 and 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, where as array[10] is invalid and index 10 is invalid, so 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.

No comments:

Post a Comment

Note: only a member of this blog may post a comment.