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
- Pass a non-null topic string (the actual topic name) when constructing the ConsumerRecord.
- If the topic comes from upstream state, validate/require it before constructing the record.
- 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
- Treat a null topic as a producer/upstream bug — log and drop the record before constructing a ConsumerRecord.
- When building test fixtures, always pass an explicit topic string.
- Centralize ConsumerRecord construction in one factory method that enforces the null check.
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
- Headers cannot be null
- Topic must be non-null.
- Failed to construct kafka consumer
- Partitions collection cannot be null
- Invalid value null for configuration key.deserializer: must
AI-assisted analysis of apache/kafka@c31c9215e1 (2026-08-03).
Data as JSON: /data/errors/fda0903711042f33.json.
Report an issue: GitHub.