{"record":{"id":"072a8dc981a6ee87","repo":"apache/flink","slug":"the-collection-contains-a-null-element-072a8d","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/IndexLookupGeneratorFunction.java","lineNumber":116,"sourceCode":"    }\n\n    @Override\n    public OUT map(Long index) throws Exception {\n        return lookupMap.get(index);\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     */\n    private void checkIterable(Iterable<OUT> elements, Class<?> viewedAs) {\n        for (OUT elem : elements) {\n            numElements++;\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    private void serializeElements(Iterable<OUT> elements) throws IOException {\n        Preconditions.checkState(serializer != null, \"serializer not set\");\n        LOG.info(\"Serializing elements using  {}\", serializer);\n        ByteArrayOutputStream baos = new ByteArrayOutputStream();\n        DataOutputViewStreamWrapper wrapper = new DataOutputViewStreamWrapper(baos);\n\n        try {\n            for (OUT element : elements) {","sourceCodeStart":98,"sourceCodeEnd":134,"githubUrl":"https://github.com/apache/flink/blob/2f3c205e9266cb30240eb7f4fdab15cad629a70f/flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/functions/IndexLookupGeneratorFunction.java#L98-L134","documentation":"Thrown by the private checkIterable method in IndexLookupGeneratorFunction when any element in the provided collection is null. This check runs in the constructor and also increments numElements per element, so it doubles as the element counter. A null element aborts construction immediately.","triggerScenarios":"Constructing new IndexLookupGeneratorFunction<>(typeInfo, elements) with an Iterable containing one or more null values.","commonSituations":"Programmatically building an element list where computation produced null; reading elements from an external source that may emit nulls; passing a collection built from nullable data without filtering.","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(\"x\", null, \"y\");\nnew IndexLookupGeneratorFunction<>(Types.STRING, elements);\n\n// after\nList<String> elements = Stream.of(\"x\", null, \"y\")\n    .filter(Objects::nonNull).collect(Collectors.toList());\nnew IndexLookupGeneratorFunction<>(Types.STRING, elements);","handlingStrategy":"validation","validationCode":"// Filter nulls before constructing IndexLookupGeneratorFunction\nList<OUT> cleanElements = StreamSupport.stream(elements.spliterator(), false)\n    .filter(Objects::nonNull)\n    .collect(Collectors.toList());\nnew IndexLookupGeneratorFunction<>(typeInfo, cleanElements);","typeGuard":null,"tryCatchPattern":"try {\n    new IndexLookupGeneratorFunction<>(typeInfo, elements);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"null element\")) {\n        elements = StreamSupport.stream(elements.spliterator(), false)\n            .filter(Objects::nonNull).collect(Collectors.toList());\n        new IndexLookupGeneratorFunction<>(typeInfo, elements);\n    } else throw e;\n}","preventionTips":["Filter nulls from collections before construction.","Validate external data sources for null values.","Use Optional wrappers if nulls represent valid semantic data."],"tags":["datagen","validation","null-check","index-lookup","constructor"],"backgroundTag":null,"analyzedSha":"2f3c205e9266cb30240eb7f4fdab15cad629a70f","analyzedAt":"2026-08-14T08:48:24.518Z","schemaVersion":2},"datasetVersion":"2026-08-14T10:17:34.591Z"}