Blankj/AndroidUtilCode · error · IndexOutOfBoundsException
Index: {}, array1 Length: {}
Error message
Index: {}, array1 Length: {} What it means
Thrown by ArrayUtils.realAddArr when concatenating two arrays (the add(array1, index, array2) family) at an insertion point that falls outside the closed range [0, array1.length]. The library guards the System.arraycopy boundaries by rejecting out-of-range indices before any allocation. Valid insertion points are 0 (prepend) through array1.length (append).
Source
Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java:771
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);
System.arraycopy(array2, 0, joinedArray, index, len2);
System.arraycopy(array1, index, joinedArray, index + len2, len1 - index);
}
return joinedArray;
}
/**
* <p>Inserts the specified element at the specified position in the array.View on GitHub (pinned to 7b4caf9e54)
Solutions
- Clamp/validate the index to 0..array1.length before calling add (index 0 prepends, index array1.length appends).
- When array1 is null or empty, force index to 0 (only index 0 is legal for an empty/null array1).
- If the intent is pure append, always pass index = array1.length (or 0 when empty).
- Add an assertion/guard in the caller computing the insertion point from external input.
Example fix
// before
byte[] a = {1, 2, 3};
byte[] b = {9};
byte[] r = ArrayUtils.add(a, 5, b); // Index: 5, array1 Length: 3
// after
int insertAt = Math.max(0, Math.min(a.length, 5));
byte[] r = ArrayUtils.add(a, insertAt, b); Defensive patterns
Strategy: validation
Validate before calling
int len1 = (array1 == null) ? 0 : array1.length; int safeIndex = Math.max(0, Math.min(index, len1)); if (len1 == 0) safeIndex = 0; // empty/null array1 only allows index 0 byte[] joined = ArrayUtils.add(array1, safeIndex, array2);
Try / catch
try {
result = ArrayUtils.add(a, idx, b);
} catch (IndexOutOfBoundsException e) {
// fall back to a safe append
result = ArrayUtils.add(a, a.length, b);
} Prevention
- Treat the insertion index as a closed range [0, array1.length]: 0 prepends, length appends.
- When array1 may be empty or null, always force index to 0.
- Never derive the index from a different array's length without clamping.
When it happens
Trigger: Calling add(byte[]/short[]/int[]/long[]/float[]/double[]/boolean[]/char[]/Object[] array1, int index, array2) with index < 0, index > array1.length, or any non-zero index when array1 is empty (length 0). Note line 763 produces the 'Length: 0' variant when array1 is empty and index != 0; line 771 produces the general 'Length: N' variant.
Common situations: Off-by-one when computing insertion index (e.g., using array.length instead of array.length as the cap is fine, but length+1 is not); passing an index that was valid for a longer array to a now-empty one; treating the insertion index as 1-based; concatenating user-supplied indices without clamping.
Related errors
- Index: {}, Length: 0
- Index: {}, Length: {}
- Not an array: {}
- Array has incompatible type: {}
- Index: {}, array1 Length: 0
AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14).
Data as JSON: /api/errors/fef7c661d69e23b8.
Report an issue: GitHub.