didi/DoKit · error · IllegalArgumentException

Not an array: " + array.getClass()

Error message

Not an array: " + array.getClass()

What it means

Thrown by ArrayUtils.forEach (the Closure-walking method) when the passed object is not any of the recognized array types (Object[], boolean[], byte[], char[], double[], float[], etc.). The method dispatches on instanceof checks over every primitive array type; anything else falls into the final else branch and is rejected. It is a defensive type check, not a runtime failure of correct input.

Source

Thrown at Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java:2106

            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 626827cddb)

Solutions

  1. Branch before calling: if (object != null && object.getClass().isArray()) use forEach, else handle Collection/String/null explicitly.
  2. For multidimensional arrays, iterate the outer dimension yourself and call forEach on each inner array.
  3. Replace the call with a plain for-each loop when the static type is known.

Example fix

// before
ArrayUtils.forEach(someObject, (i, item) -> Log.d(TAG, item + "")); // throws if not array

// after
if (someObject != null && someObject.getClass().isArray()) {
    ArrayUtils.forEach(someObject, (i, item) -> Log.d(TAG, item + ""));
} else if (someObject instanceof Collection) {
    int i = 0;
    for (Object o : (Collection<?>) someObject) Log.d(TAG, String.valueOf(o));
}
Defensive patterns

Strategy: type-guard

Validate before calling

boolean supported = object != null && object.getClass().isArray();
if (supported) ArrayUtils.forEach(object, closure);
else handleNonArray(object);

Type guard

static boolean isSingleDimensionArray(Object o) {
    if (o == null || !o.getClass().isArray()) return false;
    Class<?> comp = o.getClass().getComponentType();
    return comp.isPrimitive() || comp == Object.class || !comp.isArray();
}

Try / catch

try { ArrayUtils.forEach(obj, closure); } catch (IllegalArgumentException e) { /* obj not an array: fall back to scalar handling */ }

Prevention

When it happens

Trigger: Calling ArrayUtils.forEach(obj, closure) where obj is a Collection, String, Map, multidimensional array element typed as Object, or any non-array reference. Passing null is also unsafe here because none of the instanceof branches match null.

Common situations: Generic log/print code that accepts Object and assumes it is an array; 2D arrays (int[][]) that arrive as Object and match no single-dimension branch; refactoring that changed a field from array to List without updating the forEach call.

Related errors


AI-assisted analysis of didi/DoKit@626827cddb (2026-08-14). Data as JSON: /api/errors/62b5542fd1409727. Report an issue: GitHub.