{"record":{"id":"919c0bd134d5a37f","repo":"apache/hadoop","slug":"non-array-value-of-class-not-allowed","errorCode":null,"errorMessage":"non-array value of class {} not allowed","messagePattern":"non-array value of class (.+?) not allowed","errorType":"exception","errorClass":"HadoopIllegalArgumentException","httpStatus":null,"severity":"error","filePath":"hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/ArrayPrimitiveWritable.java","lineNumber":95,"sourceCode":"          + 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   */\n  public ArrayPrimitiveWritable() {\n    //empty constructor\n  }\n  \n  /**\n   * Construct an instance of known type but no value yet\n   * for use with type-specific wrapper classes.\n   *\n   * @param componentType componentType.\n   */\n  public ArrayPrimitiveWritable(Class<?> componentType) {","sourceCodeStart":77,"sourceCodeEnd":113,"githubUrl":"https://github.com/apache/hadoop/blob/2add9630210752f88ceb1bb74eb65e37bf41da8e/hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/ArrayPrimitiveWritable.java#L77-L113","documentation":"checkArray()'s second guard in set(Object) throws HadoopIllegalArgumentException when the value is non-null but not an array at all. ArrayPrimitiveWritable must derive componentType via value.getClass().getComponentType(), which returns null for non-arrays, so any scalar, collection, or object is rejected.","triggerScenarios":"set(Integer.valueOf(42)), set(\"abc\"), set(Arrays.asList(1,2,3)), set(new ArrayList<double[]>()) — anything where value.getClass().isArray() is false.","commonSituations":"Passing a single primitive instead of a one-element array; passing a List from business code assuming Hadoop converts it; passing a boxed value from a generic <T> method with T=Integer rather than T=Integer[].","solutions":["Wrap scalars in a one-element primitive array: set(new int[] {v}).","Convert collections to primitive arrays first (e.g. ints via stream().mapToInt(...).toArray()).","For non-primitive elements use ArrayWritable or a custom Writable."],"exampleFix":"// before\nList<Integer> nums = ...;\nw.set(nums); // throws: not an array\n\n// after\nint[] arr = nums.stream().mapToInt(Integer::intValue).toArray();\nw.set(arr);","handlingStrategy":"type-guard","validationCode":"Object v = ...;\nif (v == null || !v.getClass().isArray()) {\n  throw new IllegalArgumentException(\"Expected an array, got: \" + (v == null ? \"null\" : v.getClass().getName()));\n}","typeGuard":"static boolean isNonEmptyPrimitiveArray(Object v) {\n  return v != null && v.getClass().isArray()\n      && v.getClass().getComponentType().isPrimitive();\n}","tryCatchPattern":null,"preventionTips":["Convert Lists to primitive arrays before serialization (stream().mapToInt(...).toArray()).","Remember generic type parameters erase — a <T> that is Integer, not Integer[], will fail here."],"tags":["hadoop","serialization","writable","type-validation","array"],"backgroundTag":"argument-type-validation","analyzedSha":"2add9630210752f88ceb1bb74eb65e37bf41da8e","analyzedAt":"2026-08-22T19:55:07.957Z","schemaVersion":2},"datasetVersion":"2026-08-23T01:17:44.959Z"}