StringIndexOutOfBoundsException

java.lang.StringIndexOutOfBoundsException: String index out of range: 10

Quick Answer

You are accessing a character in a string at an index that exceeds its length. Check the string length before calling charAt() or substring().

Why This Happens

String methods like charAt(), substring(), and indexOf() throw this exception when the provided index is negative or exceeds the string length. This often happens when assuming a string has a minimum length without checking first.

The Problem

String text = "Hello";
char ch = text.charAt(10); // StringIndexOutOfBoundsException

The Fix

String text = "Hello";
if (text.length() > 10) {
    char ch = text.charAt(10);
}

Step-by-Step Fix

  1. 1

    Identify the invalid index

    Read the exception message to find which index was out of range and check the actual string length.

  2. 2

    Find the string operation

    Look at charAt(), substring(), or other string methods on the failing line to see which index argument is wrong.

  3. 3

    Add length validation

    Check the string length before accessing by index, and handle the case where the string is shorter than expected.

Bugsly catches this automatically

Bugsly's AI analyzes this error pattern in real-time, explains what went wrong in plain English, and suggests the exact fix — before your users even report it.

Try Bugsly free