{"record":{"id":"62b5542fd1409727","repo":"didi/DoKit","slug":"not-an-array-array-getclass","errorCode":null,"errorMessage":"Not an array: \" + array.getClass()","messagePattern":"Not an array: \" \\+ array\\.getClass\\(\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java","lineNumber":2106,"sourceCode":"            long[] longs = (long[]) array;\n            for (int i = 0, length = longs.length; i < length; i++) {\n                long ele = longs[i];\n                closure.execute(i, (E) Long.valueOf(ele));\n            }\n        } else if (array instanceof float[]) {\n            float[] floats = (float[]) array;\n            for (int i = 0, length = floats.length; i < length; i++) {\n                float ele = floats[i];\n                closure.execute(i, (E) Float.valueOf(ele));\n            }\n        } else if (array instanceof double[]) {\n            double[] doubles = (double[]) array;\n            for (int i = 0, length = doubles.length; i < length; i++) {\n                double ele = doubles[i];\n                closure.execute(i, (E) Double.valueOf(ele));\n            }\n        } else {\n            throw new IllegalArgumentException(\"Not an array: \" + array.getClass());\n        }\n    }\n\n    /**\n     * Return the string of array.\n     *\n     * @param array The array.\n     * @return the string of array\n     */\n    @NonNull\n    public static String toString(@Nullable Object array) {\n        if (array == null) return \"null\";\n        if (array instanceof Object[]) {\n            return Arrays.deepToString((Object[]) array);\n        } else if (array instanceof boolean[]) {\n            return Arrays.toString((boolean[]) array);\n        } else if (array instanceof byte[]) {\n            return Arrays.toString((byte[]) array);","sourceCodeStart":2088,"sourceCodeEnd":2124,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java#L2088-L2124","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Branch before calling: if (object != null && object.getClass().isArray()) use forEach, else handle Collection/String/null explicitly.","For multidimensional arrays, iterate the outer dimension yourself and call forEach on each inner array.","Replace the call with a plain for-each loop when the static type is known."],"exampleFix":"// before\nArrayUtils.forEach(someObject, (i, item) -> Log.d(TAG, item + \"\")); // throws if not array\n\n// after\nif (someObject != null && someObject.getClass().isArray()) {\n    ArrayUtils.forEach(someObject, (i, item) -> Log.d(TAG, item + \"\"));\n} else if (someObject instanceof Collection) {\n    int i = 0;\n    for (Object o : (Collection<?>) someObject) Log.d(TAG, String.valueOf(o));\n}","handlingStrategy":"type-guard","validationCode":"boolean supported = object != null && object.getClass().isArray();\nif (supported) ArrayUtils.forEach(object, closure);\nelse handleNonArray(object);","typeGuard":"static boolean isSingleDimensionArray(Object o) {\n    if (o == null || !o.getClass().isArray()) return false;\n    Class<?> comp = o.getClass().getComponentType();\n    return comp.isPrimitive() || comp == Object.class || !comp.isArray();\n}","tryCatchPattern":"try { ArrayUtils.forEach(obj, closure); } catch (IllegalArgumentException e) { /* obj not an array: fall back to scalar handling */ }","preventionTips":["Check object.getClass().isArray() before array-specific utilities.","Handle multidimensional arrays with Arrays.deepToString-style manual iteration.","Keep logging helpers generic-safe: branch on Collection vs array vs scalar."],"tags":["arrays","type-check","illegal-argument"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}