karatelabs/karate · error · java.lang.IndexOutOfBoundsException

Index: , Size: <buffer.length>

Error message

Index: <index>, Size: <buffer.length>

What it means

JsUint8Array.get(index) throws IndexOutOfBoundsException with the message 'Index: <index>, Size: <buffer.length>' when the requested index is negative or >= the underlying byte buffer length. This mirrors Java's ArrayList bounds message so it is familiar to Java developers. It means JS code read a byte position outside the array's allocated size.

Solutions

  1. Check the index against the array's length before reading.
  2. Use for (var i = 0; i < arr.length; i++) — not <=.
  3. If out-of-range reads should be tolerated, wrap the access in try/catch or use a bounds-checking helper.

Example fix

// before
var b = u8[i];
// after
if (i >= 0 && i < u8.length) { var b = u8[i]; }
Defensive patterns

Strategy: validation

Validate before calling

function canRead(arr, i) { return i >= 0 && i < arr.length; }
if (!canRead(u8, i)) throw new Error('byte index out of range: ' + i);

Type guard

function inBounds(arr, i) { return Number.isInteger(i) && i >= 0 && i < arr.length; }

Try / catch

try { b = u8[i]; } catch (e) { if (karate.match(e, '#string').length > 0 && e.indexOf('IndexOutOfBoundsException') >= 0) { b = 0; } else { throw e; } }

Prevention

When it happens

Trigger: Reading u8[i] where i < 0 or i >= u8.length, e.g. u8[u8.length] (one past the end) or u8[-1].

Common situations: Off-by-one loops (`i <= arr.length`); assuming JS arrays auto-extend like plain arrays; using an index computed from another array of a different size.

Related errors


AI-assisted analysis of karatelabs/karate@a22eb90246 (2026-09-12). Data as JSON: /api/errors/68cd3e361f2fc6ca. Report an issue: GitHub.

Appendix: source

Thrown at karate-js/src/main/java/io/karatelabs/js/JsUint8Array.java:174

    @Override
    @SuppressWarnings("unchecked")
    public <T> T[] toArray(T[] a) {
        int size = buffer.length;
        T[] result = a.length >= size ? a : (T[]) java.lang.reflect.Array.newInstance(a.getClass().getComponentType(), size);
        for (int i = 0; i < size; i++) {
            result[i] = (T) (Integer) (buffer[i] & 0xFF);
        }
        if (a.length > size) {
            result[size] = null;
        }
        return result;
    }

    @Override
    public Object get(int index) {
        if (index < 0 || index >= buffer.length) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + buffer.length);
        }
        return buffer[index] & 0xFF;
    }

    @Override
    public Object set(int index, Object element) {
        if (index < 0 || index >= buffer.length) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Size: " + buffer.length);
        }
        int previous = buffer[index] & 0xFF;
        if (element instanceof Number n) {
            buffer[index] = (byte) (n.intValue() & 0xFF);
        }
        return previous;
    }

    @Override
    public int indexOf(Object o) {

View on GitHub (pinned to a22eb90246)