apache/kafka · error · IllegalArgumentException

Topic cannot be null

Error message

Topic cannot be null

What it means

IllegalArgumentException thrown by the ConsumerRecord constructor when the topic argument is null. Kafka records are always scoped to a topic; a null topic breaks partition routing, serialization, and record equality, so construction fails fast.

Source

Thrown at clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecord.java:153

     * @param value The record contents
     * @param headers The headers of the record
     * @param leaderEpoch Optional leader epoch of the record (may be empty for legacy record formats)
     * @param deliveryCount Optional delivery count of the record (may be empty when deliveries not counted)
     */
    public ConsumerRecord(String topic,
                          int partition,
                          long offset,
                          long timestamp,
                          TimestampType timestampType,
                          int serializedKeySize,
                          int serializedValueSize,
                          K key,
                          V value,
                          Headers headers,
                          Optional<Integer> leaderEpoch,
                          Optional<Short> deliveryCount) {
        if (topic == null)
            throw new IllegalArgumentException("Topic cannot be null");
        if (headers == null)
            throw new IllegalArgumentException("Headers cannot be null");

        this.topic = topic;
        this.partition = partition;
        this.offset = offset;
        this.timestamp = timestamp;
        this.timestampType = timestampType;
        this.serializedKeySize = serializedKeySize;
        this.serializedValueSize = serializedValueSize;
        this.key = key;
        this.value = value;
        this.headers = headers;
        this.leaderEpoch = leaderEpoch;
        this.deliveryCount = deliveryCount;
    }

    /**

View on GitHub (pinned to c31c9215e1)

Solutions

  1. Pass a non-null topic string (the actual topic name) when constructing the ConsumerRecord.
  2. If the topic comes from upstream state, validate/require it before constructing the record.
  3. In test code, use a constant dummy topic name like "test-topic" rather than null.

Example fix

// before
new ConsumerRecord<>(null, 0, 0L, "key", "value");

// after
new ConsumerRecord<>("test-topic", 0, 0L, "key", "value");
Defensive patterns

Strategy: validation

Validate before calling

if (topic == null) {
    throw new IllegalArgumentException("topic must be non-null before constructing ConsumerRecord");
}
new ConsumerRecord<>(topic, partition, offset, key, value);

Try / catch

try {
    new ConsumerRecord<>(topic, partition, offset, key, value);
} catch (IllegalArgumentException e) {
    if ("Topic cannot be null".equals(e.getMessage())) {
        // fall back to a sentinel/default topic or reject the upstream record
    }
    throw e;
}

Prevention

When it happens

Trigger: Directly instantiating ConsumerRecord with a null topic (test fixtures, custom deserializers that synthesize records, interceptors that rebuild records); passing a null topic name through producer/consumer helper code.

Common situations: Unit tests building ConsumerRecord stubs with placeholder nulls; custom deserializers or interceptors that drop the topic; wrapper libraries that construct records from incomplete sources.

Related errors


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