didi/DoKit · error · IndexOutOfBoundsException

Index: " + index + ", array1 Length: 0

Error message

Index: " + index + ", array1 Length: 0

What it means

ArrayUtils.realAddArr implements 'insert array2 into array1 at index'. When array1 is empty (length 0), insertion is only legal at index 0 (prepend == append); any other index is out of range, so it throws IndexOutOfBoundsException('Index: N, array1 Length: 0').

Source

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

        if (result == null) return null;
        return (float[]) result;
    }

    @Nullable
    public static double[] add(@Nullable double[] array1, int index, @Nullable double[] array2) {
        Object result = realAddArr(array1, index, array2, Double.TYPE);
        if (result == null) return null;
        return (double[]) result;
    }

    @Nullable
    private static Object realAddArr(@Nullable Object array1, int index, @Nullable Object array2, Class clss) {
        if (array1 == null && array2 == null) return null;
        int len1 = getLength(array1);
        int len2 = getLength(array2);
        if (len1 == 0) {
            if (index != 0) {
                throw new IndexOutOfBoundsException("Index: " + index + ", array1 Length: 0");
            }
            return realCopy(array2);
        }
        if (len2 == 0) {
            return realCopy(array1);
        }
        if (index > len1 || index < 0) {
            throw new IndexOutOfBoundsException("Index: " + index + ", array1 Length: " + len1);
        }
        Object joinedArray = Array.newInstance(array1.getClass().getComponentType(), len1 + len2);
        if (index == len1) {
            System.arraycopy(array1, 0, joinedArray, 0, len1);
            System.arraycopy(array2, 0, joinedArray, len1, len2);
        } else if (index == 0) {
            System.arraycopy(array2, 0, joinedArray, 0, len2);
            System.arraycopy(array1, 0, joinedArray, len2, len1);
        } else {
            System.arraycopy(array1, 0, joinedArray, 0, index);

View on GitHub (pinned to 626827cddb)

Solutions

  1. When the target array is empty, pass index 0 (or use add(array, array2) appending)
  2. Clamp the index: int safeIndex = array1.length == 0 ? 0 : Math.min(index, array1.length)
  3. Initialize array1 with the first element instead of starting from an empty array if index semantics matter

Example fix

// before
double[] result = ArrayUtils.add(new double[0], insertPos, values);

// after
double[] result = ArrayUtils.add(new double[0], Math.min(insertPos, 0), values);
Defensive patterns

Strategy: validation

Validate before calling

int safeIndex = (array1 == null || array1.length == 0) ? 0 : index;
int[] out = ArrayUtils.add(array1, safeIndex, array2);

Prevention

When it happens

Trigger: Calling any ArrayUtils.add(array1, index, array2) overload (int[], long[], Object[], etc.) with an empty first array and a non-zero index — e.g. defaulting an index to array2.length or a computed insertion point that assumed array1 was non-empty.

Common situations: Building arrays incrementally where the first insertion uses a computed position (cursor position, drop index) that is non-zero on the first call. Config defaults creating empty arrays combined with user-supplied indices.

Related errors


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