{"record":{"id":"ee8702e5967529b8","repo":"didi/DoKit","slug":"array-has-incompatible-type-array-getclass","errorCode":null,"errorMessage":"Array has incompatible type: \" + array.getClass()","messagePattern":"Array has incompatible type: \" \\+ array\\.getClass\\(\\)","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java","lineNumber":2138,"sourceCode":"            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);\n        } else if (array instanceof char[]) {\n            return Arrays.toString((char[]) array);\n        } else if (array instanceof double[]) {\n            return Arrays.toString((double[]) array);\n        } else if (array instanceof float[]) {\n            return Arrays.toString((float[]) array);\n        } else if (array instanceof int[]) {\n            return Arrays.toString((int[]) array);\n        } else if (array instanceof long[]) {\n            return Arrays.toString((long[]) array);\n        } else if (array instanceof short[]) {\n            return Arrays.toString((short[]) array);\n        }\n        throw new IllegalArgumentException(\"Array has incompatible type: \" + array.getClass());\n    }\n\n    public interface Closure<E> {\n        void execute(int index, E item);\n    }\n}\n","sourceCodeStart":2120,"sourceCodeEnd":2145,"githubUrl":"https://github.com/didi/DoKit/blob/626827cddb2feb2f3aee87a52a064b4e5ca2bed4/Android/dokit-util/src/main/java/com/didichuxing/doraemonkit/util/ArrayUtils.java#L2120-L2145","documentation":"Thrown by ArrayUtils.toString(Object) after its instanceof chain over boolean[], byte[], char[], double[], float[], int[], long[], short[] and Object[] fails. Only single-dimension arrays of those exact types are supported; every other runtime type (nested arrays, custom objects, non-array references) reaches the throw. Correct callers never see it; it guards against unsupported array shapes.","triggerScenarios":"Calling ArrayUtils.toString on a multidimensional array such as int[][] or String[][] (the outer array is typed Object[] only if the inner arrays are Object[]), or on a non-array Object. Nested primitive arrays like float[][] do not match float[] and throw.","commonSituations":"Debug/dump utilities that stringify arbitrary fields via reflection; matrix or coordinate data stored as int[][]; generics erasure delivering an unexpected runtime type to a logging helper.","solutions":["Use java.util.Arrays.deepToString(...) for nested arrays instead of ArrayUtils.toString.","Guard with object.getClass().isArray() && object.getClass().getComponentType().isPrimitive() || component type is Object before calling.","If the value may not be an array at all, String.valueOf(object) is the safe generic fallback."],"exampleFix":"// before\nint[][] grid = {{1,2},{3,4}};\nString s = ArrayUtils.toString(grid); // throws\n\n// after\nint[][] grid = {{1,2},{3,4}};\nString s = java.util.Arrays.deepToString(grid);","handlingStrategy":"type-guard","validationCode":"if (object != null && object.getClass().isArray()\n        && !object.getClass().getComponentType().isArray()) {\n    s = ArrayUtils.toString(object);\n} else {\n    s = java.util.Arrays.deepToString((Object[]) object); // nested\n}","typeGuard":"static boolean isFlatPrimitiveOrObjectArray(Object o) {\n    if (o == null || !o.getClass().isArray()) return false;\n    return !o.getClass().getComponentType().isArray();\n}","tryCatchPattern":"try { text = ArrayUtils.toString(value); } catch (IllegalArgumentException e) { text = String.valueOf(value); }","preventionTips":["Use Arrays.deepToString for anything possibly nested.","String.valueOf is the universal safe stringify fallback.","Encapsulate a dump(Object) helper that dispatches by type instead of calling toString directly."],"tags":["arrays","tostring","illegal-argument"],"backgroundTag":null,"analyzedSha":"626827cddb2feb2f3aee87a52a064b4e5ca2bed4","analyzedAt":"2026-08-14T12:45:58.758Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}