Follow Us
Contribute
  • Register

Java.lang.StringIndexOutOfBoundsException

+1 vote

This exception is thrown in java when the index is higher than the size of the string. Ex:
String str = "abcd";
str.charAt(5);

The above example will throw StringIndexOutOfBoundsException, because the string contains only 4 character elements and the index is 5.

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

1 Solution

+1 vote

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.StringIndexOutOfBoundsException: String index out of range: 5
at java.lang.String.charAt(Unknown Source)
at SomePackage.SomeClass.main(SomeClass.java:11)

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

solved 7 years ago by errorbase (170,010 points)
edited 7 years ago by onur

Related errors