{"record":{"id":"91c85f3edb6529c8","repo":"apache/hadoop","slug":"input-array-component-type-does-not-match-decla","errorCode":null,"errorMessage":"input array component type {} does not match declared type {}","messagePattern":"input array component type (.+?) does not match declared type (.+?)","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/ArrayPrimitiveWritable.java","lineNumber":84,"sourceCode":"  \n  private static Class<?> getPrimitiveClass(String className) {\n    return PRIMITIVE_NAMES.get(className);\n  }\n  \n  private static void checkPrimitive(Class<?> componentType) {\n    if (componentType == null) { \n      throw new HadoopIllegalArgumentException(\"null component type not allowed\"); \n    }\n    if (! PRIMITIVE_NAMES.containsKey(componentType.getName())) {\n      throw new HadoopIllegalArgumentException(\"input array component type \"\n          + componentType.getName() + \" is not a candidate primitive type\");\n    }\n  }\n  \n  private void checkDeclaredComponentType(Class<?> componentType) {\n    if ((declaredComponentType != null) \n        && (componentType != declaredComponentType)) {\n      throw new HadoopIllegalArgumentException(\"input array component type \"\n          + componentType.getName() + \" does not match declared type \"\n          + declaredComponentType.getName());     \n    }\n  }\n  \n  private static void checkArray(Object value) {\n    if (value == null) { \n      throw new HadoopIllegalArgumentException(\"null value not allowed\"); \n    }\n    if (! value.getClass().isArray()) {\n      throw new HadoopIllegalArgumentException(\"non-array value of class \"\n          + value.getClass() + \" not allowed\");             \n    }\n  }\n  \n  /**\n   * Construct an empty instance, for use during Writable read\n   */","sourceCodeStart":66,"sourceCodeEnd":102,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/ArrayPrimitiveWritable.java#L66-L102","documentation":"An ArrayPrimitiveWritable can pin itself to a declared component type via the ArrayPrimitiveWritable(Class<?>) constructor (used by type-specific wrappers). checkDeclaredComponentType() throws when set() is later called with an array whose runtime component type differs from that declared type — identity comparison (==), not isAssignableFrom.","triggerScenarios":"new ArrayPrimitiveWritable(Integer.TYPE) followed by writable.set(new long[10]); or a type-specific wrapper (e.g. a LongArrayWritable that declared Long.TYPE) reused via set() with an int[] value.","commonSituations":"Reusing a typed writable instance across fields of different primitive types in a loop; generic framework code that pools writables but feeds arrays whose type varies per record.","solutions":["Match the array type to the declared type exactly (int[] with Integer.TYPE, long[] with Long.TYPE).","Create a fresh untyped ArrayPrimitiveWritable(value) per array instead of reusing a typed instance.","Guard with isDeclaredComponentType(array.getClass().getComponentType()) before calling set()."],"exampleFix":"// before\nArrayPrimitiveWritable w = new ArrayPrimitiveWritable(Integer.TYPE);\nw.set(new long[] {1L}); // throws: long != int\n\n// after\nArrayPrimitiveWritable w = new ArrayPrimitiveWritable(Integer.TYPE);\nw.set(new int[] {1});","handlingStrategy":"validation","validationCode":"Class<?> arrType = arr.getClass().getComponentType();\nif (w.isDeclaredComponentType(arrType)) {\n  w.set(arr);\n} else {\n  w = new ArrayPrimitiveWritable(arr); // fresh untyped instance\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Use isDeclaredComponentType() before set() on typed instances.","Do not reuse a typed ArrayPrimitiveWritable for arrays of a different primitive type."],"tags":["hadoop","serialization","writable","type-mismatch","validation"],"backgroundTag":"type-mismatch","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-22T20:17:22.307Z"}