{"record":{"id":"cda1bfd6e167e3ac","repo":"Blankj/AndroidUtilCode","slug":"index-length-0","errorCode":null,"errorMessage":"Index: {}, Length: 0","messagePattern":"Index: (.+?), Length: 0","errorType":"exception","errorClass":"IndexOutOfBoundsException","httpStatus":null,"severity":"error","filePath":"lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java","lineNumber":873,"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":855,"sourceCodeEnd":891,"githubUrl":"https://github.com/Blankj/AndroidUtilCode/blob/7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809/lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java#L855-L891","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["When the array may be null, set index to 0 (the only legal value for a null array).","Initialize the accumulator to an empty typed array (new byte[0]) instead of null if you need non-zero indices.","Guard with: if (array == null) index = 0; before calling add.","Use the index from a prior indexOf/size only after confirming the array is non-null."],"exampleFix":"// before\nbyte[] acc = null;\nacc = ArrayUtils.add(acc, 1, (byte) 7); // Index: 1, Length: 0\n\n// after\nint idx = (acc == null) ? 0 : 1;\nacc = ArrayUtils.add(acc, idx, (byte) 7);","handlingStrategy":"validation","validationCode":"int safeIndex = (array == null) ? 0 : index;\nbyte[] r = ArrayUtils.add(array, safeIndex, element);","typeGuard":null,"tryCatchPattern":"try {\n    r = ArrayUtils.add(array, index, element);\n} catch (IndexOutOfBoundsException e) {\n    r = ArrayUtils.add(array, 0, element); // null array => index 0 only\n}","preventionTips":["For null arrays the only legal index is 0; default to it.","Initialize accumulators to new T[0] rather than null when you need non-zero indices.","Reset reused indices to 0 whenever the array becomes null."],"tags":["array","index-out-of-bounds","null-array","arrayutils","java"],"backgroundTag":null,"analyzedSha":"7b4caf9e5440046b3fefb63b6b6e2ead7ebaf809","analyzedAt":"2026-08-14T02:26:54.956Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}