libgdx/libgdx · error · IndexOutOfBoundsException

IndexOutOfBoundsException

Error message

IndexOutOfBoundsException

What it means

DoubleArrayBuffer.get(int index) throws IndexOutOfBoundsException when index < 0 or index >= limit. Absolute gets are validated against the buffer's limit (not capacity), per the java.nio contract, so even in-bounds-capacity indexes beyond the limit are rejected. This protects the backing array access backingArray[offset + index] from going out of range.

Source

Thrown at backends/gdx-backends-gwt/src/com/badlogic/gdx/backends/gwt/emu/java/nio/DoubleArrayBuffer.java:58

		this(capacity, new double[capacity], 0);
	}

	DoubleArrayBuffer (int capacity, double[] backingArray, int offset) {
		super(capacity);
		this.backingArray = backingArray;
		this.offset = offset;
	}

	public final double get () {
		if (position == limit) {
			throw new BufferUnderflowException();
		}
		return backingArray[offset + position++];
	}

	public final double get (int index) {
		if (index < 0 || index >= limit) {
			throw new IndexOutOfBoundsException();
		}
		return backingArray[offset + index];
	}

	public final DoubleBuffer get (double[] dest, int off, int len) {
		int length = dest.length;
		if (off < 0 || len < 0 || (long)off + (long)len > length) {
			throw new IndexOutOfBoundsException();
		}
		if (len > remaining()) {
			throw new BufferUnderflowException();
		}
		System.arraycopy(backingArray, offset + position, dest, off, len);
		position += len;
		return this;
	}

	public final boolean isDirect () {

View on GitHub (pinned to 97f4086187)

Solutions

  1. Iterate against limit: for (int i = 0; i < buf.limit(); i++) ...
  2. Clamp/validate index before use: if (index < 0 || index >= buf.limit()) throw/log.
  3. After slicing or flipping, recompute indexes relative to the new buffer's own coordinate space.

Example fix

// before
for (int i = 0; i < buf.capacity(); i++) sum += buf.get(i); // may throw

// after
for (int i = 0; i < buf.limit(); i++) sum += buf.get(i);
Defensive patterns

Strategy: validation

Validate before calling

// validate against limit, not capacity
if (index >= 0 && index < buf.limit()) {
  double v = buf.get(index);
} else {
  throw new IllegalArgumentException("index " + index + " outside [0," + buf.limit() + ")");
}

Prevention

When it happens

Trigger: Calling get(i) with i >= limit — e.g. index computed from capacity after a flip()/slice() reduced the limit, or negative index from a subtraction that underflowed. The check fires before any array access.

Common situations: Iterating with for (i=0; i<buf.capacity(); i++) buf.get(i) after limit was set smaller; using an index relative to a parent buffer on a slice; arithmetic producing -1 used as an index.

Related errors


AI-assisted analysis of libgdx/libgdx@97f4086187 (2026-08-14). Data as JSON: /api/errors/301e4f7a71ae54a3. Report an issue: GitHub.