apache/kafka · error · java.lang.IllegalArgumentException

Topic partitions to assign to cannot have null or empty topi

Error message

Topic partitions to assign to cannot have null or empty topic

What it means

Thrown by assign(Collection<TopicPartition>) when iterating the partitions collection encounters a TopicPartition that is itself null or whose topic() returns a blank string (checked via isBlank). The client cannot route fetches without a concrete topic name, so it refuses the assignment.

Source

Thrown at clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java:619

            log.info("Unsubscribed all topics or patterns and assigned partitions");
        } finally {
            release();
        }
    }

    @Override
    public void assign(Collection<TopicPartition> partitions) {
        acquireAndEnsureOpen();
        try {
            if (partitions == null) {
                throw new IllegalArgumentException("Topic partition collection to assign to cannot be null");
            } else if (partitions.isEmpty()) {
                this.unsubscribe();
            } else {
                for (TopicPartition tp : partitions) {
                    String topic = (tp != null) ? tp.topic() : null;
                    if (isBlank(topic))
                        throw new IllegalArgumentException("Topic partitions to assign to cannot have null or empty topic");
                }
                fetcher.clearBufferedDataForUnassignedPartitions(partitions);

                // make sure the offsets of topic partitions the consumer is unsubscribing from
                // are committed since there will be no following rebalance
                if (coordinator != null)
                    this.coordinator.maybeAutoCommitOffsetsAsync(time.milliseconds());

                log.info("Assigned to partition(s): {}", partitions.stream().map(TopicPartition::toString).collect(Collectors.joining(", ")));
                if (this.subscriptions.assignFromUser(new HashSet<>(partitions)))
                    metadata.requestUpdateForNewTopics();
            }
        } finally {
            release();
        }
    }

    @Override

View on GitHub (pinned to c31c9215e1)

Solutions

  1. Filter out null and blank-topic partitions before assign: partitions.removeIf(tp -> tp == null || isBlank(tp.topic())).
  2. Validate topic names at construction time so TopicPartition is never built with a blank topic.
  3. Log skipped entries during partition resolution so misconfiguration is visible.

Example fix

// before
List<TopicPartition> parts = topics.stream()
    .map(t -> t == null ? null : new TopicPartition(t, 0))
    .collect(Collectors.toList());
consumer.assign(parts);

// after
List<TopicPartition> parts = topics.stream()
    .filter(Objects::nonNull)
    .filter(t -> !t.trim().isEmpty())
    .map(t -> new TopicPartition(t, 0))
    .collect(Collectors.toList());
consumer.assign(parts);
Defensive patterns

Strategy: validation

Validate before calling

java.util.Collection<org.apache.kafka.common.TopicPartition> partitions = /* ... */;
for (org.apache.kafka.common.TopicPartition tp : partitions) {
    if (tp == null || tp.topic() == null || tp.topic().trim().isEmpty()) {
        throw new IllegalArgumentException("Topic partitions to assign to cannot have null or empty topic");
    }
}
consumer.assign(partitions);

Type guard

static boolean isValidTopicPartition(org.apache.kafka.common.TopicPartition tp) {
    return tp != null && tp.topic() != null && !tp.topic().trim().isEmpty()
        && tp.partition() >= 0;
}

boolean allValid = partitions.stream().allMatch(YourClass::isValidTopicPartition);

Try / catch

try {
    consumer.assign(partitions);
} catch (IllegalArgumentException e) {
    if (e.getMessage().contains("null or empty topic")) {
        partitions = partitions.stream()
            .filter(tp -> tp != null && tp.topic() != null && !tp.topic().isEmpty())
            .collect(java.util.stream.Collectors.toList());
        consumer.assign(partitions);
    } else throw e;
}

Prevention

When it happens

Trigger: Passing a collection containing a null element (e.g. a List with nulls), or constructing TopicPartition("", 0) / TopicPartition(null, 0). Stream pipelines that filter into a list leaving null placeholders.

Common situations: Parsing topic names from an external source where some entries are blank. Off-by-one when building partitions from a partition count. Using a map keyed by topic where a key was deleted.

Related errors


AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03). Data as JSON: /data/errors/1148b6cd4ae08000.json. Report an issue: GitHub.