Blankj/AndroidUtilCode · error · IndexOutOfBoundsException
Index: {}, array1 Length: 0
Error message
Index: {}, array1 Length: 0 What it means
ArrayUtils.realAddArr throws IndexOutOfBoundsException("Index: N, array1 Length: 0") when the first array is empty (length 0) and the requested insertion index is not 0. With an empty target array, only index 0 is valid; any other index has no position to insert at.
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java:763
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 7b4caf9e54)
Solutions
- When array1 is empty, pass index 0 (the only valid position) or simply use array2 directly.
- Clamp/validate the index to [0, len1] before calling add: if len1==0 force index=0.
- Prefer the single-arg ArrayUtils.add(array1, array2) (append) when you only need concatenation, to avoid manual indexing.
Example fix
// before
int[] empty = new int[0];
int[] out = ArrayUtils.add(empty, 2, new int[]{7}); // throws: Index: 2, array1 Length: 0
// after
int[] out = ArrayUtils.add(empty, 0, new int[]{7}); // valid: only index 0 on empty array Defensive patterns
Strategy: validation
Validate before calling
private static int[] safeAdd(int[] a1, int index, int[] a2) {
int len1 = a1 == null ? 0 : a1.length;
if (len1 == 0) index = 0; // only valid position for an empty array
return ArrayUtils.add(a1, index, a2);
} Type guard
private static boolean isValidAddIndex(Object array1, int index) {
int len = java.lang.reflect.Array.getLength(array1);
return index >= 0 && index <= len && (len != 0 || index == 0);
} Prevention
- When array1 is empty, pass index 0 only.
- Prefer the append overload ArrayUtils.add(array1, array2) for plain concatenation.
- Clamp the insertion index to [0, len1] before calling the indexed add.
When it happens
Trigger: Calling any typed ArrayUtils.add(emptyArray, index, array2) overload (Object[], int[], byte[], etc.) where array1.length==0 and index != 0. The private realAddArr is shared by every typed add overload.
Common situations: Passing an empty array plus a non-zero index from a variable that defaulted to -1 or a stale position; merging into an array that was filtered to empty without resetting the insertion index; off-by-one where callers assume len1-based index is allowed.
Related errors
- Invalid orientation. It should be either HORIZONTAL or VERTI
- MediaType is not correct: "{}"
- u can't instantiate me...
- Index: {}, array1 Length: {}
- Index: {}, Length: 0
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/ff300d4569960323.
Report an issue: GitHub.