{"record":{"id":"356f3f81e5f4b151","repo":"apache/flink","slug":"the-elements-in-the-collection-are-not-all-subclas","errorCode":null,"errorMessage":"The elements in the collection are not all subclasses of {viewedAs.getCanonicalName()}","messagePattern":"The elements in the collection are not all subclasses of (.+?)","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/functions/FromElementsGeneratorFunction.java","lineNumber":195,"sourceCode":"    //  Utilities\n    // ------------------------------------------------------------------------\n\n    /**\n     * Verifies that all elements in the iterable are non-null, and are of the given class, or a\n     * subclass thereof.\n     *\n     * @param elements The iterable to check.\n     * @param viewedAs The class to which the elements must be assignable to.\n     * @param <OUT> The generic type of the iterable to be checked.\n     */\n    public static <OUT> void checkIterable(Iterable<OUT> elements, Class<?> viewedAs) {\n        for (OUT elem : elements) {\n            if (elem == null) {\n                throw new IllegalArgumentException(\"The collection contains a null element\");\n            }\n\n            if (!viewedAs.isAssignableFrom(elem.getClass())) {\n                throw new IllegalArgumentException(\n                        \"The elements in the collection are not all subclasses of \"\n                                + viewedAs.getCanonicalName());\n            }\n        }\n    }\n}\n","sourceCodeStart":177,"sourceCodeEnd":202,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/functions/FromElementsGeneratorFunction.java#L177-L202","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","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."],"exampleFix":"// before: TypeInformation says String but list has an Integer\nList<Serializable> mixed = Arrays.asList(\"a\", 42);\nnew FromElementsGeneratorFunction<>(Types.STRING, mixed);\n\n// after: use consistent types\nList<String> elements = Arrays.asList(\"a\", \"b\");\nnew FromElementsGeneratorFunction<>(Types.STRING, elements);","handlingStrategy":"validation","validationCode":"// Verify all elements are assignable before constructing\nClass<?> expected = typeInfo.getTypeClass();\nfor (OUT elem : elements) {\n    if (elem == null || !expected.isAssignableFrom(elem.getClass())) {\n        throw new IllegalArgumentException(\n            \"Element \" + elem + \" is not assignable to \" + expected);\n    }\n}","typeGuard":"static <T> boolean allAssignable(Iterable<T> elements, Class<?> viewedAs) {\n    for (T elem : elements) {\n        if (elem == null || !viewedAs.isAssignableFrom(elem.getClass())) return false;\n    }\n    return true;\n}","tryCatchPattern":null,"preventionTips":["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."],"tags":["datagen","type-system","validation","type-mismatch"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}