apache/flink · error · IllegalArgumentException

Collection must not contain null elements

Error message

Collection must not contain null elements

What it means

Thrown by extractTypeInfoFromCollection when the first element of the collection passed to fromCollection is null. Type extraction reflects on the first element's class; a null element gives it nothing to inspect, so it fails fast with IllegalArgumentException.

Source

Thrown at flink-datastream/src/main/java/org/apache/flink/datastream/impl/ExecutionEnvironmentImpl.java:236

    }

    public CheckpointConfig getCheckpointCfg() {
        return checkpointCfg;
    }

    // -----------------------------------------------
    //              Internal Methods
    // -----------------------------------------------

    private static <OUT> TypeInformation<OUT> extractTypeInfoFromCollection(Collection<OUT> data) {
        Preconditions.checkNotNull(data, "Collection must not be null");
        if (data.isEmpty()) {
            throw new IllegalArgumentException("Collection must not be empty");
        }

        OUT first = data.iterator().next();
        if (first == null) {
            throw new IllegalArgumentException("Collection must not contain null elements");
        }

        TypeInformation<OUT> typeInfo;
        try {
            typeInfo = TypeExtractor.getForObject(first);
        } catch (Exception e) {
            throw new RuntimeException(
                    "Could not create TypeInformation for type "
                            + first.getClass()
                            + "; please specify the TypeInformation manually via the version of the "
                            + "method that explicitly accepts it as an argument.",
                    e);
        }
        return typeInfo;
    }

    @SuppressWarnings("unchecked")
    private static <OUT, T extends TypeInformation<OUT>> T getSourceTypeInfo(

View on GitHub (pinned to 2f3c205e92)

Solutions

  1. Filter nulls before submission: data.stream().filter(Objects::nonNull).collect(toList())
  2. Fix the producer of the collection so it never inserts null placeholders
  3. Use fromCollection(data, explicitTypeInformation) so extraction does not depend on the first element

Example fix

// before
env.fromCollection(Arrays.asList(null, "a", "b"));

// after
List<String> clean = Arrays.asList(null, "a", "b").stream()
        .filter(Objects::nonNull)
        .collect(Collectors.toList());
env.fromCollection(clean);
Defensive patterns

Strategy: validation

Validate before calling

List<OUT> clean = data.stream().filter(Objects::nonNull).collect(Collectors.toList());
if (clean.isEmpty()) throw new IllegalArgumentException("no non-null elements");
env.fromCollection(clean);

Prevention

When it happens

Trigger: env.fromCollection(Arrays.asList(null, event1, event2)) — any collection whose first element (data.iterator().next()) is null. Note: only the FIRST element matters; nulls later in the collection do not trigger this check here.

Common situations: Arrays.asList(null, ...) patterns, lists built with add(null) placeholders, or map/filter chains upstream of submission that produce a null head element.

Related errors


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