Blankj/AndroidUtilCode · error · IllegalArgumentException

Not an array: {}

Error message

Not an array: {}

What it means

Thrown by ArrayUtils.forAllDo when the first argument is non-null but is not one of the recognized array types (Object[], boolean[], byte[], char[], short[], int[], long[], float[], double[]). The method iterates the closure over each element; null and null closure return early (line 2049), so reaching this throw means a non-array object was passed.

Source

Thrown at lib/utilcode/src/main/java/com/blankj/utilcode/util/ArrayUtils.java:2105

            long[] longs = (long[]) array;
            for (int i = 0, length = longs.length; i < length; i++) {
                long ele = longs[i];
                closure.execute(i, (E) Long.valueOf(ele));
            }
        } else if (array instanceof float[]) {
            float[] floats = (float[]) array;
            for (int i = 0, length = floats.length; i < length; i++) {
                float ele = floats[i];
                closure.execute(i, (E) Float.valueOf(ele));
            }
        } else if (array instanceof double[]) {
            double[] doubles = (double[]) array;
            for (int i = 0, length = doubles.length; i < length; i++) {
                double ele = doubles[i];
                closure.execute(i, (E) Double.valueOf(ele));
            }
        } else {
            throw new IllegalArgumentException("Not an array: " + array.getClass());
        }
    }

    /**
     * Return the string of array.
     *
     * @param array The array.
     * @return the string of array
     */
    @NonNull
    public static String toString(@Nullable Object array) {
        if (array == null) return "null";
        if (array instanceof Object[]) {
            return Arrays.deepToString((Object[]) array);
        } else if (array instanceof boolean[]) {
            return Arrays.toString((boolean[]) array);
        } else if (array instanceof byte[]) {
            return Arrays.toString((byte[]) array);

View on GitHub (pinned to 7b4caf9e54)

Solutions

  1. Convert collections to arrays first: forAllDo(list.toArray(), closure).
  2. Use CollectionUtils.forEach for Collection/Iterator/Map inputs.
  3. Add an isArray() type guard before calling forAllDo.
  4. If the value may be null, let it pass through (null is handled gracefully); only non-array non-null values fail.

Example fix

// before
ArrayUtils.forAllDo(new ArrayList<>(), closure); // Not an array: class ArrayList

// after
ArrayUtils.forAllDo(list.toArray(), closure);
Defensive patterns

Strategy: type-guard

Validate before calling

if (array != null && array.getClass().isArray()) {
    ArrayUtils.forAllDo(array, closure);
} else if (array instanceof Collection) {
    ArrayUtils.forAllDo(((Collection<?>) array).toArray(), closure);
}

Type guard

static boolean isArray(Object o) {
    return o != null && o.getClass().isArray();
}

Try / catch

try {
    ArrayUtils.forAllDo(obj, closure);
} catch (IllegalArgumentException e) {
    if (obj instanceof Collection) ArrayUtils.forAllDo(((Collection<?>) obj).toArray(), closure);
}

Prevention

When it happens

Trigger: Passing a Collection (List, Set), a Map, a String, a boxed primitive, or any POJO to forAllDo instead of a real array. Also triggered by passing a multi-dimensional array component that is itself a non-iterable scalar.

Common situations: Confusing forAllDo with CollectionUtils.forEach; passing list.toArray() incorrectly (e.g., a single element instead of the array); reflective code feeding an arbitrary Object into forAllDo.

Related errors


AI-assisted analysis of Blankj/AndroidUtilCode@7b4caf9e54 (2026-08-14). Data as JSON: /api/errors/20853738e4be524d. Report an issue: GitHub.