didi/DoKit · error · IndexOutOfBoundsException

Index: " + index + ", Length: 0

Error message

Index: " + index + ", Length: 0

What it means

ArrayUtils.realAdd implements single-element insertion (add(array, index, element)). When the array is null it is treated as length 0, and inserting into an empty/null array is only valid at index 0; any non-zero index throws IndexOutOfBoundsException('Index: N, Length: 0').

Source

Thrown at Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java:874

    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 626827cddb)

Solutions

  1. When the array is null/empty, pass index 0
  2. Normalize the index up front: int safeIndex = (array == null || array.length == 0) ? 0 : index
  3. Initialize collections/arrays to empty instead of null so length-based indices stay consistent

Example fix

// before
float[] updated = ArrayUtils.add(nullArray, 3, value);

// after
float[] updated = ArrayUtils.add(nullArray, 0, value);
Defensive patterns

Strategy: validation

Validate before calling

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

Prevention

When it happens

Trigger: Calling any ArrayUtils.add(type[] array, int index, value) overload with a null array and index != 0 — e.g. appending to a null array using a stale index, or using array.length as the index after initializing the array to null instead of an empty array.

Common situations: Fields initialized to null that code appends to with a computed index. Migration from a List-based implementation where add(index, x) tolerated different semantics. Index defaults carried over from non-empty states.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/f0886c7f73c11fc9. Report an issue: GitHub.