liveBook · Manning

IndexOutOfBoundsException

As explained in 100 Java Mistakes and How to Avoid Them

IndexOutOfBoundsException is a Java runtime exception thrown when an index is outside the valid range of a sequence or indexed data structure.

IndexOutOfBoundsException marks an invalid position in indexed data. Java checks array, string, and list accesses, preventing silent corruption, incorrect behavior, and security holes, but the exception still signals a bug that callers should prevent. section 5.2

Empty input

Calling charAt(0) before checking that a string is nonempty fails for an empty string. Check the input boundary before accessing the character. section 5.2

Prevent the common boundary-checking mistakes:

  • Check before indexing, not after it. section 5.2
  • Use index < length, not a reversed comparison; an index equal to length is invalid. section 5.2

How 100 Java Mistakes and How to Avoid Them covers it

How deep it goes
Explains failure causes, three faulty guard patterns, prevention helpers, and analyzer coverage.
Examples
Uses empty-string charAt access, faulty array guards, and Objects checks in collection and stream implementations.
What you can do after
Diagnose invalid accesses and prevent them with ordered checks, extracted expressions, and standard validation methods.
How it connects
Connects arrays, strings, lists, off-by-one errors, bounds checking, and static analysis.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of 100 Java Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.