apache/flink · error · IllegalArgumentException

The collection contains a null element

Error message

The collection contains a null element

What it means

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.

Source

Thrown at flink-connectors/flink-connector-datagen/src/main/java/org/apache/flink/connector/datagen/functions/FromElementsGeneratorFunction.java:191

        }
    }

    // ------------------------------------------------------------------------
    //  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

  1. Filter null elements from the collection before constructing the function.
  2. If nulls represent valid data, wrap them in an Optional or a sentinel object.
  3. Validate the collection upstream before passing it to the constructor.

Example fix

// before
List<String> elements = Arrays.asList("a", null, "b");
new FromElementsGeneratorFunction<>(Types.STRING, elements);

// after
List<String> elements = Arrays.asList("a", null, "b")
    .stream().filter(Objects::nonNull).collect(Collectors.toList());
new FromElementsGeneratorFunction<>(Types.STRING, elements);
Defensive patterns

Strategy: validation

Validate before calling

// Filter nulls before constructing
List<OUT> cleanElements = new ArrayList<>();
for (OUT elem : elements) {
    if (elem != null) cleanElements.add(elem);
}
// Use cleanElements in the constructor

Try / catch

try {
    new FromElementsGeneratorFunction<>(typeInfo, elements);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("null element")) {
        elements = elements.stream().filter(Objects::nonNull).collect(Collectors.toList());
        new FromElementsGeneratorFunction<>(typeInfo, elements);
    } else throw e;
}

Prevention

When it happens

Trigger: 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.

Common situations: 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").

Related errors


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