Saturday, November 23, 2019

Common Reasons Of Java.Lang.Arrayindexoutofboundsexception Inwards Java

The ArrayIndexOutOfBoundsException, besides known equally java.lang.ArrayIndexOutOfBoundsExcepiton is ane 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[] cite = {"abc"} in addition to so trying to access name[1] volition grade java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 mistake because index 1 is invalid here. Why? because index inwards Java array starts amongst nix rather than 1, thus inwards an array of simply ane chemical factor the alone valid index is index zero.  This was ane of the most mutual scenarios which motility several 1000000 of ArrayIndexOutOfBoundsException daily. Sometimes it simply a patently programming mistake but sometimes if an array is populated exterior in addition to if at that topographic point is an mistake on feed than besides you lot acquire java.lang.ArrayIndexOutOfBoundsException inwards Java.

In this article, I volition portion amongst you lot around of the mutual reasons of ArrayIndexOutOfBoundsExcpetion in addition to their solution. This volition grade you lot plenty sense to bargain amongst this mistake inwards future.

In guild to solve ArrayIndexOutOfBoundsException, simply think next key details almost array inwards Java:

1) The array index inwards Java starts at nix in addition to goes to length - 1, for instance inwards an integer array int[] primes = novel int[10], the commencement index would live on nix in addition to end index out live on ix (10 -1)

2) Array index cannot live on negative, thus prime[-1] volition throw java.lang.ArrayIndexOutOfBoundsException.

3) The maximum array index tin live on Integer.MAX_VALUE -1 because the information type of index is int inwards Java in addition to 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 almost array information construction inwards Java. 

Now, let's run across around 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 nix it agency the array is empty in addition to the index is nix agency nosotros are trying to access the first chemical factor of the array. This happens when you lot elbow grease 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 publish = array[0];      // proficient code     if (array.length > 0) {       publish = 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 employment is to ever access array subsequently checking array length, equally shown inwards the proficient code snippet.


ArrayIndexOutOfBoundsExcepiton is ane 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 minute 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 elbow grease to access index = length in addition to so you lot acquire this error. This ordinarily happens when you lot purpose <= or >= sign inwards for loop piece 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];     }      // proficient code     for (int i = 0; i < array.length; i++) {       amount = amount + array[i];     }   }  }

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

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 alone from 0 to 9. See a proficient information construction mass e.g. Introduction to Algorithm to larn to a greater extent than almost array information construction inwards general.



java.lang.ArrayIndexOutOfBoundsException length=12 index=-1
This is the tertiary mutual reasons of millions of ArrayIndexOutOfBoundsException. In this case, you lot are trying to access the array using a negative index. There could live on dissimilar reasons why index becomes negative but the bottom employment is you lot should non access an array amongst the negative index.


That's all almost 3 examples of ArrayIndexOutOfBoundsException inwards Java. You tin solve this mistake really easily If you lot think the key details almost array implementation inwards Java e.g. array index starting at nix in addition to no negative indexes are allowed. You should besides know that the maximum an index tin boot the bucket upward 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 compass Unsupported major.minor version 52.0 inwards Java in addition to 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 compass java.lang.ClassNotFoundException: org.postgresql.Driver mistake inwards Java? [solution]
  • How to compass variable mightiness non accept been initialized mistake inwards Java? (solution)
  • How to fix Caused By: java.lang.NoClassDefFoundError: org/apache/log4j/Logger inwards Java? (fix)
  • java.sql.SQLException: No suitable driver flora for jdbc:jtds:sqlserver (solution)
  • Cause in addition to solution of java.lang.ClassNotFoundException: com.mysql.jdbc.Driver (solution)

Further Learning
Complete Java Masterclass
Java Fundamentals: The Java Language
Java In-Depth: Become a Complete Java Engineer!

No comments:

Post a Comment