{"record":{"id":"f0886c7f73c11fc9","repo":"didi/DoKit","slug":"index-index-length-0","errorCode":null,"errorMessage":"Index: \" + index + \", Length: 0","messagePattern":"Index: \" \\+ index \\+ \", Length: 0","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java","lineNumber":874,"sourceCode":"    public static long[] add(@Nullable long[] array, int index, long element) {\n        return (long[]) realAdd(array, index, element, Long.TYPE);\n    }\n\n    @NonNull\n    public static float[] add(@Nullable float[] array, int index, float element) {\n        return (float[]) realAdd(array, index, element, Float.TYPE);\n    }\n\n    @NonNull\n    public static double[] add(@Nullable double[] array, int index, double element) {\n        return (double[]) realAdd(array, index, element, Double.TYPE);\n    }\n\n    @NonNull\n    private static Object realAdd(@Nullable Object array, int index, @Nullable Object element, Class clss) {\n        if (array == null) {\n            if (index != 0) {\n                throw new IndexOutOfBoundsException(\"Index: \" + index + \", Length: 0\");\n            }\n            Object joinedArray = Array.newInstance(clss, 1);\n            Array.set(joinedArray, 0, element);\n            return joinedArray;\n        }\n        int length = Array.getLength(array);\n        if (index > length || index < 0) {\n            throw new IndexOutOfBoundsException(\"Index: \" + index + \", Length: \" + length);\n        }\n        Object result = Array.newInstance(clss, length + 1);\n        System.arraycopy(array, 0, result, 0, index);\n        Array.set(result, index, element);\n        if (index < length) {\n            System.arraycopy(array, index, result, index + 1, length - index);\n        }\n        return result;\n    }\n","sourceCodeStart":856,"sourceCodeEnd":892,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java#L856-L892","documentation":"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').","triggerScenarios":"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.","commonSituations":"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.","solutions":["When the array is null/empty, pass index 0","Normalize the index up front: int safeIndex = (array == null || array.length == 0) ? 0 : index","Initialize collections/arrays to empty instead of null so length-based indices stay consistent"],"exampleFix":"// before\nfloat[] updated = ArrayUtils.add(nullArray, 3, value);\n\n// after\nfloat[] updated = ArrayUtils.add(nullArray, 0, value);","handlingStrategy":"validation","validationCode":"int safeIndex = (array == null) ? 0 : index;\nint[] out = ArrayUtils.add(array, safeIndex, element);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Prefer empty arrays over null defaults so index arithmetic stays valid","Treat null arrays as length-0 and force index 0"],"tags":["arrayutils","doraemonkit","index-out-of-bounds","null-array","java"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}