Ad Space — Top Banner

StringIndexOutOfBoundsException

Java Programming Language

Severity: Minor

What Does This Error Mean?

A StringIndexOutOfBoundsException means your code tried to access a character position in a String that does not exist. Strings in Java are indexed starting at 0, and the last valid position is length minus 1. If you ask for position 5 in a 5-character string, you get this error because position 5 is past the end. This often happens when working with substrings, character extraction, or string slicing.

Affected Models

  • Java 8
  • Java 11
  • Java 17
  • Java 21
  • All Java versions

Common Causes

  • Calling charAt() with an index equal to or greater than the string's length
  • Calling substring(start, end) where end is greater than the string length, or start is greater than end
  • Using a hardcoded index that works for one string but fails when a shorter string is used
  • An empty string — any charAt() call on an empty string immediately throws this error
  • Off-by-one error in a loop that processes characters one by one and goes one step too far

How to Fix It

  1. Read the error message — it tells you the invalid index and the string length. For example: 'String index out of range: 5' on a string of length 5 means you accessed index 5 (valid range: 0 to 4).

    This is the same off-by-one pattern as ArrayIndexOutOfBoundsException — the last valid index is always length minus 1.

  2. Before calling charAt(index), check: if (index >= 0 && index < str.length()). Before calling substring(start, end), check: start >= 0, end >= start, and end <= str.length().

    Note: substring(start, end) uses the end index exclusively — substring(0, 5) gives you characters at positions 0, 1, 2, 3, 4.

  3. Handle the empty string case separately. An empty string has length 0, so any index access on it will fail. Check str.isEmpty() or str.length() > 0 before working with individual characters.

    Null strings also cause problems — call charAt() on null and you get a NullPointerException. Check for both: if (str != null && !str.isEmpty()).

  4. For substring operations on strings whose length you do not know in advance, clamp the end index: int end = Math.min(desiredEnd, str.length()). This prevents going past the end.

    Example: str.substring(0, Math.min(50, str.length())) safely gets up to the first 50 characters without crashing on shorter strings.

  5. Use String methods that avoid manual indexing where possible. Methods like contains(), startsWith(), endsWith(), indexOf(), and split() handle the index arithmetic for you.

    Regular expressions via str.matches() or str.replaceAll() are also excellent for complex string parsing that would otherwise require risky manual indexing.

When to Call a Professional

StringIndexOutOfBoundsException is always fixable in your code. The error message includes the bad index and the string length. Add bounds checks before accessing specific character positions.

Frequently Asked Questions

What is the difference between substring(start) and substring(start, end)?

substring(start) returns everything from start to the end of the string. substring(start, end) returns characters from start up to (but not including) end. Both can throw StringIndexOutOfBoundsException if start or end are out of range.

Why does Java use 0-based indexing for strings?

For the same reason arrays start at 0 — it maps to how memory offsets work and is consistent with every other indexed structure in Java. Once you internalize 'last valid index is length minus 1', the pattern becomes second nature.

Is there a safe version of charAt() that does not throw?

Not in the standard library — but you can write one: if (index >= 0 && index < str.length()) return str.charAt(index); else return a default character. Or convert to a char array with toCharArray() and use Arrays to work with it more safely.