RSS
Follow Us
Contribute
 
 
 
 
Print Preferences

Java.lang.ArrayIndexOutOfBoundsException

 

From $1

Table of contents
  1. 1. Cause
  2. 2. Solution

Cause

This exception is thrown in java when the index is higher than the size of the array. Ex:

int[] arr = {1, 2, 3, 4, 5};
int x = arr[6];

The above example will throw ArrayIndexOutOfBoundsException, because the array contains only 5 integer elements and the index is 6.

Solution

The thrown exception shows the line number in which the problem has occured. The exception thrown for the above example is like this:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 6
	at SomePackage.SomeClass.main(SomeClass.java:9)

For this example the problem has occured in line 9. By this way you can reach the variable which created the problem. After finding the variable, debug the code for that variable.

 
 
 
 
 
Creative Commons License
All content is licensed under a Creative Commons Attribution-Noncommercial 3.0 License.
Powered by MindTouch Core v.9.02.2