{"record":{"id":"8f9b4db035c7f455","repo":"apache/flink","slug":"the-collection-contains-a-null-element","errorCode":null,"errorMessage":"The collection contains a null element","messagePattern":"The collection contains a null element","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":191,"sourceCode":"        }\n    }\n\n    // ------------------------------------------------------------------------\n    //  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":173,"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#L173-L202","documentation":"Thrown by the static checkIterable validation method in FromElementsGeneratorFunction when any element in the provided collection is null. This check runs in the constructor (and again in setOutputType for backward-compatible legacy usage) before elements are serialized, enforcing that all elements are non-null.","triggerScenarios":"Constructing FromElementsGeneratorFunction with an Iterable or varargs array that contains one or more null values. Also triggered in setOutputType which re-invokes checkIterable with the stored elements.","commonSituations":"Building an element list programmatically where a computation produced null; reading elements from an external source that may emit nulls; using Arrays.asList(\"a\", null, \"b\").","solutions":["Filter null elements from the collection before constructing the function.","If nulls represent valid data, wrap them in an Optional or a sentinel object.","Validate the collection upstream before passing it to the constructor."],"exampleFix":"// before\nList<String> elements = Arrays.asList(\"a\", null, \"b\");\nnew FromElementsGeneratorFunction<>(Types.STRING, elements);\n\n// after\nList<String> elements = Arrays.asList(\"a\", null, \"b\")\n    .stream().filter(Objects::nonNull).collect(Collectors.toList());\nnew FromElementsGeneratorFunction<>(Types.STRING, elements);","handlingStrategy":"validation","validationCode":"// Filter nulls before constructing\nList<OUT> cleanElements = new ArrayList<>();\nfor (OUT elem : elements) {\n    if (elem != null) cleanElements.add(elem);\n}\n// Use cleanElements in the constructor","typeGuard":null,"tryCatchPattern":"try {\n    new FromElementsGeneratorFunction<>(typeInfo, elements);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"null element\")) {\n        elements = elements.stream().filter(Objects::nonNull).collect(Collectors.toList());\n        new FromElementsGeneratorFunction<>(typeInfo, elements);\n    } else throw e;\n}","preventionTips":["Always filter nulls from dynamically-built element collections.","Use @NonNull annotations or static analysis to catch null propagation early.","Validate external data sources before feeding elements to the generator."],"tags":["datagen","validation","null-check","constructor"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}