Follow Us
Contribute
  • Register

Java.lang.IndexOutOfBoundsException

+2 votes

This exception is thrown in java when the index is higher than the size of a vector, arraylist, array etc... Ex:
ArrayList arr = new ArrayList();
arr.add("someStringHere");
for (int i = 0 ; i < 5 ; i++) {
arr.get(i);
}

The above example will throw IndexOutOfBoundsException, because the arraylist contains only 1 string and the index is going further than 0.  (Also in Java the indices start with 0.)

requested 3 years ago by errorbase (170,010 points)
edited 3 years ago by onur

1 Solution

0 votes

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.IndexOutOfBoundsException: Index: 1, Size: 1
at java.util.ArrayList.RangeCheck(Unknown Source)
at java.util.ArrayList.get(Unknown Source)
at SomePackage.SomeClass.main(SomeClass.java:16)

For this example the problem has occured in line 16. Additionally Index: 1 shows the index that we tried to reach and Size: 1 tells us the actual size of the variable.

solved 3 years ago by errorbase (170,010 points)
edited 3 years ago by onur
java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

Related errors

+1 vote
1 solution
0 votes
1 solution
0 votes
1 solution
0 votes
0 solutions