Blankj/AndroidUtilCode · error · IndexOutOfBoundsException

Index: {}, Length: 0

Error message

Index: {}, Length: 0

What it means

Thrown by ArrayUtils.realAdd when inserting a single element via add(array, index, element) but the input array is null and the requested index is not 0. A null array is treated as length 0, so the only legal insertion index is 0; any other index is rejected before the single-element array is allocated.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java:873

    public static long[] add(@Nullable long[] array, int index, long element) {
        return (long[]) realAdd(array, index, element, Long.TYPE);
    }

    @NonNull
    public static float[] add(@Nullable float[] array, int index, float element) {
        return (float[]) realAdd(array, index, element, Float.TYPE);
    }

    @NonNull
    public static double[] add(@Nullable double[] array, int index, double element) {
        return (double[]) realAdd(array, index, element, Double.TYPE);
    }

    @NonNull
    private static Object realAdd(@Nullable Object array, int index, @Nullable Object element, Class clss) {
        if (array == null) {
            if (index != 0) {
                throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0");
            }
            Object joinedArray = Array.newInstance(clss, 1);
            Array.set(joinedArray, 0, element);
            return joinedArray;
        }
        int length = Array.getLength(array);
        if (index > length || index < 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
        }
        Object result = Array.newInstance(clss, length + 1);
        System.arraycopy(array, 0, result, 0, index);
        Array.set(result, index, element);
        if (index < length) {
            System.arraycopy(array, index, result, index + 1, length - index);
        }
        return result;
    }

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. When the array may be null, set index to 0 (the only legal value for a null array).
  2. Initialize the accumulator to an empty typed array (new byte[0]) instead of null if you need non-zero indices.
  3. Guard with: if (array == null) index = 0; before calling add.
  4. Use the index from a prior indexOf/size only after confirming the array is non-null.

Example fix

// before
byte[] acc = null;
acc = ArrayUtils.add(acc, 1, (byte) 7); // Index: 1, Length: 0

// after
int idx = (acc == null) ? 0 : 1;
acc = ArrayUtils.add(acc, idx, (byte) 7);
Defensive patterns

Strategy: validation

Validate before calling

int safeIndex = (array == null) ? 0 : index;
byte[] r = ArrayUtils.add(array, safeIndex, element);

Try / catch

try {
    r = ArrayUtils.add(array, index, element);
} catch (IndexOutOfBoundsException e) {
    r = ArrayUtils.add(array, 0, element); // null array => index 0 only
}

Prevention

When it happens

Trigger: Calling add(byte[]/char[]/short[]/int[]/long[]/float[]/double[]/boolean[] array, int index, element) with array == null and index != 0. Common when reusing an index computed against a previously-non-null array that is now null.

Common situations: Building an array incrementally from null (e.g., byte[] acc = null; acc = add(acc, i, v)) but using an incrementing index i that starts above 0; passing a configurable index whose array argument was not initialized; defaulting array to null but defaulting index to 1.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/cda1bfd6e167e3ac. Report an issue: GitHub.