apache/flink · error · IllegalArgumentException

Invalid element type for a primitive array.

Error message

Invalid element type for a primitive array.

What it means

Thrown by Types.PRIMITIVE_ARRAY(TypeInformation) when the supplied elementType is not one of the eight supported primitive element types (BOOLEAN, BYTE, SHORT, INT, LONG, FLOAT, DOUBLE, CHAR). The method uses reference equality (==) against the canonical Types constants, so even an equivalent TypeInformation that is not the exact singleton instance will fail.

Source

Thrown at flink-core/src/main/java/org/apache/flink/api/common/typeinfo/Types.java:386

    public static TypeInformation<?> PRIMITIVE_ARRAY(TypeInformation<?> elementType) {
        if (elementType == BOOLEAN) {
            return PrimitiveArrayTypeInfo.BOOLEAN_PRIMITIVE_ARRAY_TYPE_INFO;
        } else if (elementType == BYTE) {
            return PrimitiveArrayTypeInfo.BYTE_PRIMITIVE_ARRAY_TYPE_INFO;
        } else if (elementType == SHORT) {
            return PrimitiveArrayTypeInfo.SHORT_PRIMITIVE_ARRAY_TYPE_INFO;
        } else if (elementType == INT) {
            return PrimitiveArrayTypeInfo.INT_PRIMITIVE_ARRAY_TYPE_INFO;
        } else if (elementType == LONG) {
            return PrimitiveArrayTypeInfo.LONG_PRIMITIVE_ARRAY_TYPE_INFO;
        } else if (elementType == FLOAT) {
            return PrimitiveArrayTypeInfo.FLOAT_PRIMITIVE_ARRAY_TYPE_INFO;
        } else if (elementType == DOUBLE) {
            return PrimitiveArrayTypeInfo.DOUBLE_PRIMITIVE_ARRAY_TYPE_INFO;
        } else if (elementType == CHAR) {
            return PrimitiveArrayTypeInfo.CHAR_PRIMITIVE_ARRAY_TYPE_INFO;
        }
        throw new IllegalArgumentException("Invalid element type for a primitive array.");
    }

    /**
     * Returns type information for Java arrays of object types (such as <code>String[]</code>,
     * <code>Integer[]</code>). The array itself must not be null. Null values for elements are
     * supported.
     *
     * @param elementType element type of the array
     */
    @SuppressWarnings("unchecked")
    public static <E> TypeInformation<E[]> OBJECT_ARRAY(TypeInformation<E> elementType) {
        if (elementType == Types.STRING) {
            return (TypeInformation) BasicArrayTypeInfo.STRING_ARRAY_TYPE_INFO;
        }
        return ObjectArrayTypeInfo.getInfoFor(elementType);
    }

    /**

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Use only one of Types.BOOLEAN, Types.BYTE, Types.SHORT, Types.INT, Types.LONG, Types.FLOAT, Types.DOUBLE, Types.CHAR as the elementType.
  2. If you need an array of objects (String[], Integer[], POJO[]), use Types.OBJECT_ARRAY(elementType) instead.
  3. If the element type is dynamic, switch on its BasicTypeInfo/SqlTimeTypeInfo class and fall back to OBJECT_ARRAY for non-primitives.

Example fix

// before — String is not a primitive element type
TypeInformation<?> ti = Types.PRIMITIVE_ARRAY(Types.STRING); // throws

// after — use OBJECT_ARRAY for object arrays
TypeInformation<String[]> ti = Types.OBJECT_ARRAY(Types.STRING); // ok

// correct primitive array usage
TypeInformation<int[]> ti = Types.PRIMITIVE_ARRAY(Types.INT); // ok
Defensive patterns

Strategy: validation

Validate before calling

private static final Set<TypeInformation<?>> PRIMITIVE_ELEMENT_TYPES = Set.of(
        Types.BOOLEAN, Types.BYTE, Types.SHORT, Types.INT,
        Types.LONG, Types.FLOAT, Types.DOUBLE, Types.CHAR
);

if (!PRIMITIVE_ELEMENT_TYPES.contains(elementType)) {
    // use OBJECT_ARRAY for non-primitive element types
    ti = Types.OBJECT_ARRAY(elementType);
} else {
    ti = Types.PRIMITIVE_ARRAY(elementType);
}

Try / catch

try {
    ti = Types.PRIMITIVE_ARRAY(elementType);
} catch (IllegalArgumentException e) {
    // element type is not a primitive — fall back to object array
    ti = Types.OBJECT_ARRAY(elementType);
}

Prevention

When it happens

Trigger: Passing Types.STRING, Types.SQL_DATE, Types.LOCAL_DATE_TIME, a POJO/ROW/TUPLE type, or a custom TypeInformation to PRIMITIVE_ARRAY. Also triggered by passing a TypeInformation that represents a boxed type wrapper created via a different path (e.g. BasicTypeInfo.INT_TYPE_INFO instead of Types.INT — though these happen to be the same singleton, any non-singleton equivalent would fail).

Common situations: Confusing PRIMITIVE_ARRAY (int[]) with OBJECT_ARRAY (Integer[]). Passing Types.STRING expecting a String[] (use OBJECT_ARRAY instead). Constructing element type info dynamically and not narrowing to one of the eight primitives. Reading element type from a schema descriptor that yields a non-primitive.

Related errors


AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14). Data as JSON: /api/errors/3eaf93d07287ea1a. Report an issue: GitHub.