Common reasons of java.lang.ArrayIndexOutOfBoundsException inwards Java

The ArrayIndexOutOfBoundsException, too known equally java.lang.ArrayIndexOutOfBoundsExcepiton is 1 of the most mutual errors inwards Java program. It occurs when Java programme tries to access an invalid index e.g. an index which is non positive or greater than the length of an array. For example, if you lot accept an array of String e.g. String[] advert = {"abc"} as well as so trying to access name[1] volition rank java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 fault because index 1 is invalid here. Why? because index inwards Java array starts alongside zip rather than 1, so inwards an array of only 1 chemical component the solely valid index is index zero.  This was 1 of the most mutual scenarios which crusade several meg of ArrayIndexOutOfBoundsException daily. Sometimes it only a evidently programming fault but sometimes if an array is populated exterior as well as if at that spot is an fault on feed than too you lot acquire java.lang.ArrayIndexOutOfBoundsException inwards Java.

In this article, I volition portion alongside you lot to a greater extent than or less of the mutual reasons of ArrayIndexOutOfBoundsExcpetion as well as their solution. This volition rank you lot plenty sense to bargain alongside this fault inwards future.

In companionship to solve ArrayIndexOutOfBoundsException, only retrieve next fundamental details virtually array inwards Java:

1) The array index inwards Java starts at zip as well as goes to length - 1, for illustration inwards an integer array int[] primes = novel int[10], the showtime index would hold upwards zip as well as final index out hold upwards ix (10 -1)

2) Array index cannot hold upwards negative, so prime[-1] volition throw java.lang.ArrayIndexOutOfBoundsException.

3) The maximum array index tin hold upwards Integer.MAX_VALUE -1 because the information type of index is int inwards Java as well as the maximum value of int primitive is Integer.MAX_VALUE. See Big Java: Early Objects, fifth edition past times Cay S. Horstmann to larn to a greater extent than virtually array information construction inwards Java. 

Now, let's encounter to a greater extent than or less of the mutual examples of ArrayIndexOutOfBoundsException inwards Java




java.lang.ArrayIndexOutOfBoundsException length=0 index=0

This is the classic instance of accessing an empty array. Since length is zip it way the array is empty as well as the index is zip way nosotros are trying to access the first chemical component of the array. This happens when you lot attempt to access an array without checking its length, equally shown below.

public class Test {    public static void main(String[] args) {      int[] array = new int[0];      // bad code     int seat out = array[0];      // skilful code     if (array.length > 0) {       seat out = array[0];     }   }  }

When you lot run this programme you lot volition acquire next error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0 at Test.main(Main.java:15)

Influenza A virus subtype H5N1 solution to this work is to e'er access array afterwards checking array length, equally shown inwards the skilful code snippet.


ArrayIndexOutOfBoundsExcepiton is 1 of the most mutual errors inwards Java programme Common reasons of java.lang.ArrayIndexOutOfBoundsException inwards Java


java.lang.ArrayIndexOutOfBoundsException: length=1; index=1

This the mo most pop argue of ArrayIndexOutOfBoundsException inwards Java. Here you lot are trying to access an array past times its length. If you lot remember, the maximum valid index inwards the array is length -1 but if you lot somehow attempt to access index = length as well as so you lot acquire this error. This commonly happens when you lot purpose <= or >= sign inwards for loop acre looping over an array in Java.

public class ABC {    public static void main(String[] args) {      int[] array = new int[10];     int amount = 0;      // bad code     for (int i = 0; i <= array.length; i++) {       amount = amount + array[i];     }      // skilful code     for (int i = 0; i < array.length; i++) {       amount = amount + array[i];     }   }  }

If you lot run this code, you lot volition acquire next fault :

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10 at ABC.main(Main.java:16)

This is because 10 is non a valid index for an array of length 10. The valid index is solely from 0 to 9. See a skilful information construction mass e.g. Introduction to Algorithm to larn to a greater extent than virtually array information construction inwards general.



java.lang.ArrayIndexOutOfBoundsException length=12 index=-1
This is the 3rd mutual reasons of millions of ArrayIndexOutOfBoundsException. In this case, you lot are trying to access the array using a negative index. There could hold upwards unlike reasons why index becomes negative but the bottom work is you lot should non access an array alongside the negative index.


That's all virtually 3 examples of ArrayIndexOutOfBoundsException inwards Java. You tin solve this fault rattling easily If you lot retrieve the fundamental details virtually array implementation inwards Java e.g. array index starting at zip as well as no negative indexes are allowed. You should too know that the maximum an index tin transcend away upwards to Integer.MAX_VALUE value because the information type of index is int inwards Java.


Other Java troubleshooting guides you lot may like:
  • How to ready Unsupported major.minor version 52.0 inwards Java as well as Eclipse? (fix)
  • General Guide to solve java.lang.ClassNotFoundException inwards Java [guide]
  • How to solve java.lang.ClassNotFoundException:org.Springframework.Web.Context.ContextLoaderListener [solution]
  • Exception inwards thread "main" java.lang.IllegalStateException during Iterator.remove() (fix)
  • How to connect to MySQL database from Java Program [steps]
  • How to solve java.lang.ClassNotFoundException: com.mysql.jdbc.Driver inwards Java MySQL? [solution]
  • How to avoid ConcurrentModificationException inwards Java? (solution)
  • java.lang.ClassNotFoundException: org.apache.commons.logging.LogFactory? [solution]
  • How to solve java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver inwards Java? [solution]
  • How to fix java.lang.ClassNotFoundException: com.microsoft.sqlserver.jdbc.SQLServerDriver inwards Java? [solution]
  • How to ready java.lang.ClassNotFoundException: org.postgresql.Driver fault inwards Java? [solution]
  • How to ready variable mightiness non accept been initialized fault inwards Java? (solution)
  • How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger inwards Java? (fix)
  • java.sql.SQLException: No suitable driver institute for jdbc:jtds:sqlserver (solution)
  • Cause as well as solution of java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (solution)

Subscribe to receive free email updates:

0 Response to "Common reasons of java.lang.ArrayIndexOutOfBoundsException inwards Java"

Posting Komentar