Java error: ArrayIndexOutOfBoundsException

Error reference

Java error: ArrayIndexOutOfBoundsException

This page is part of Intro to Java with Greenfoot. It assumes you have reached lesson 5.1. Opening it is not counted against you and is not tracked as progress.

The message

java.lang.ArrayIndexOutOfBoundsException: Index 5 out of bounds for length 5

What it actually means

You asked an array for a slot it does not have. The array is fine; the index is wrong.

The usual causes

Ranked by how often each one turns out to be the answer. Work down the list.

  1. A loop using <= instead of <, so it runs one past the end. The message shows an index exactly equal to the length.
  2. Using a.length as an index instead of a.length - 1.
  3. On a 2D array, the two subscripts are swapped and the grid is not square.
  4. A level or wave counter that has grown past the last entry in its array.

How to fix it

Read the message: it names the index you tried AND the length. If the index equals the length exactly, change <= to < in the loop, or use a.length - 1 for the last element. On a 2D array, check that row is the FIRST subscript.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]