apache/hadoop · error · HadoopIllegalArgumentException

input array component type {} does not match declared type {

Error message

input array component type {} does not match declared type {}

What it means

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.

Source

Thrown at hadoop-common-project/hadoop-common/src/main/java/org/apache/hadoop/io/ArrayPrimitiveWritable.java:84

  
  private static Class<?> getPrimitiveClass(String className) {
    return PRIMITIVE_NAMES.get(className);
  }
  
  private static void checkPrimitive(Class<?> componentType) {
    if (componentType == null) { 
      throw new HadoopIllegalArgumentException("null component type not allowed"); 
    }
    if (! PRIMITIVE_NAMES.containsKey(componentType.getName())) {
      throw new HadoopIllegalArgumentException("input array component type "
          + componentType.getName() + " is not a candidate primitive type");
    }
  }
  
  private void checkDeclaredComponentType(Class<?> componentType) {
    if ((declaredComponentType != null) 
        && (componentType != declaredComponentType)) {
      throw new HadoopIllegalArgumentException("input array component type "
          + componentType.getName() + " does not match declared type "
          + declaredComponentType.getName());     
    }
  }
  
  private static void checkArray(Object value) {
    if (value == null) { 
      throw new HadoopIllegalArgumentException("null value not allowed"); 
    }
    if (! value.getClass().isArray()) {
      throw new HadoopIllegalArgumentException("non-array value of class "
          + value.getClass() + " not allowed");             
    }
  }
  
  /**
   * Construct an empty instance, for use during Writable read
   */

View on GitHub (pinned to 2add963021)

Solutions

  1. Match the array type to the declared type exactly (int[] with Integer.TYPE, long[] with Long.TYPE).
  2. Create a fresh untyped ArrayPrimitiveWritable(value) per array instead of reusing a typed instance.
  3. Guard with isDeclaredComponentType(array.getClass().getComponentType()) before calling set().

Example fix

// before
ArrayPrimitiveWritable w = new ArrayPrimitiveWritable(Integer.TYPE);
w.set(new long[] {1L}); // throws: long != int

// after
ArrayPrimitiveWritable w = new ArrayPrimitiveWritable(Integer.TYPE);
w.set(new int[] {1});
Defensive patterns

Strategy: validation

Validate before calling

Class<?> arrType = arr.getClass().getComponentType();
if (w.isDeclaredComponentType(arrType)) {
  w.set(arr);
} else {
  w = new ArrayPrimitiveWritable(arr); // fresh untyped instance
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of apache/hadoop@2add963021 (2026-08-22). Data as JSON: /api/errors/91c85f3edb6529c8. Report an issue: GitHub.