apache/flink · error · IllegalArgumentException
The elements in the collection are not all subclasses of {vi
Error message
The elements in the collection are not all subclasses of {viewedAs.getCanonicalName()} What it means
Thrown by checkIterable in FromElementsGeneratorFunction when an element's runtime class is not assignable to the class derived from the provided TypeInformation. This catches type mismatches before serialization to prevent corrupt or failed serialization. The message includes the canonical name of the expected type.
Source
Thrown at flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/functions/FromElementsGeneratorFunction.java:195
// Utilities
// ------------------------------------------------------------------------
/**
* Verifies that all elements in the iterable are non-null, and are of the given class, or a
* subclass thereof.
*
* @param elements The iterable to check.
* @param viewedAs The class to which the elements must be assignable to.
* @param <OUT> The generic type of the iterable to be checked.
*/
public static <OUT> void checkIterable(Iterable<OUT> elements, Class<?> viewedAs) {
for (OUT elem : elements) {
if (elem == null) {
throw new IllegalArgumentException("The collection contains a null element");
}
if (!viewedAs.isAssignableFrom(elem.getClass())) {
throw new IllegalArgumentException(
"The elements in the collection are not all subclasses of "
+ viewedAs.getCanonicalName());
}
}
}
}
View on GitHub (pinned to 2f3c205e92)
Solutions
- Ensure all elements match the declared TypeInformation's type class.
- Use Flink's Types utility (Types.STRING, Types.INT, etc.) or explicit TypeInformation to avoid type inference ambiguity.
- Inspect the canonical name in the error message to see what type was expected, then fix the mismatched element.
Example fix
// before: TypeInformation says String but list has an Integer
List<Serializable> mixed = Arrays.asList("a", 42);
new FromElementsGeneratorFunction<>(Types.STRING, mixed);
// after: use consistent types
List<String> elements = Arrays.asList("a", "b");
new FromElementsGeneratorFunction<>(Types.STRING, elements); Defensive patterns
Strategy: validation
Validate before calling
// Verify all elements are assignable before constructing
Class<?> expected = typeInfo.getTypeClass();
for (OUT elem : elements) {
if (elem == null || !expected.isAssignableFrom(elem.getClass())) {
throw new IllegalArgumentException(
"Element " + elem + " is not assignable to " + expected);
}
} Type guard
static <T> boolean allAssignable(Iterable<T> elements, Class<?> viewedAs) {
for (T elem : elements) {
if (elem == null || !viewedAs.isAssignableFrom(elem.getClass())) return false;
}
return true;
} Prevention
- Use strongly typed collections (List<String>, not List<Object>).
- Let Flink infer TypeInformation from the data, or specify it explicitly with Types.STRING etc.
- Avoid raw-type collections that can hold heterogeneous elements.
When it happens
Trigger: Passing TypeInformation.of(String.class) but including Integer elements in the collection; or passing a superclass TypeInformation while the actual elements have an incompatible runtime type; heterogeneous raw-type list pollution.
Common situations: Type erasure causing the compiler to accept a heterogeneous list; accidental type widening; generic collection pollution where a raw-type list contains mixed types; TypeInformation auto-inference picking the wrong class.
Related errors
- The elements in the collection are not all subclasses of {vi
- The collection contains a null element
- The collection contains a null element
- This type ({ffd.getType()}) cannot be used as key.
- Could not stop with a savepoint job "{}".
AI-assisted analysis of apache/flink@2f3c205e92 (2026-08-14).
Data as JSON: /api/errors/356f3f81e5f4b151.
Report an issue: GitHub.