{"id":"fda0903711042f33","repo":"apache/kafka","slug":"topic-cannot-be-null","errorCode":null,"errorMessage":"Topic cannot be null","messagePattern":"Topic cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecord.java","lineNumber":153,"sourceCode":"     * @param value The record contents\n     * @param headers The headers of the record\n     * @param leaderEpoch Optional leader epoch of the record (may be empty for legacy record formats)\n     * @param deliveryCount Optional delivery count of the record (may be empty when deliveries not counted)\n     */\n    public ConsumerRecord(String topic,\n                          int partition,\n                          long offset,\n                          long timestamp,\n                          TimestampType timestampType,\n                          int serializedKeySize,\n                          int serializedValueSize,\n                          K key,\n                          V value,\n                          Headers headers,\n                          Optional<Integer> leaderEpoch,\n                          Optional<Short> deliveryCount) {\n        if (topic == null)\n            throw new IllegalArgumentException(\"Topic cannot be null\");\n        if (headers == null)\n            throw new IllegalArgumentException(\"Headers cannot be null\");\n\n        this.topic = topic;\n        this.partition = partition;\n        this.offset = offset;\n        this.timestamp = timestamp;\n        this.timestampType = timestampType;\n        this.serializedKeySize = serializedKeySize;\n        this.serializedValueSize = serializedValueSize;\n        this.key = key;\n        this.value = value;\n        this.headers = headers;\n        this.leaderEpoch = leaderEpoch;\n        this.deliveryCount = deliveryCount;\n    }\n\n    /**","sourceCodeStart":135,"sourceCodeEnd":171,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecord.java#L135-L171","documentation":"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.","triggerScenarios":"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.","commonSituations":"Unit tests building ConsumerRecord stubs with placeholder nulls; custom deserializers or interceptors that drop the topic; wrapper libraries that construct records from incomplete sources.","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."],"exampleFix":"// before\nnew ConsumerRecord<>(null, 0, 0L, \"key\", \"value\");\n\n// after\nnew ConsumerRecord<>(\"test-topic\", 0, 0L, \"key\", \"value\");","handlingStrategy":"validation","validationCode":"if (topic == null) {\n    throw new IllegalArgumentException(\"topic must be non-null before constructing ConsumerRecord\");\n}\nnew ConsumerRecord<>(topic, partition, offset, key, value);","typeGuard":null,"tryCatchPattern":"try {\n    new ConsumerRecord<>(topic, partition, offset, key, value);\n} catch (IllegalArgumentException e) {\n    if (\"Topic cannot be null\".equals(e.getMessage())) {\n        // fall back to a sentinel/default topic or reject the upstream record\n    }\n    throw e;\n}","preventionTips":["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."],"tags":["consumer","consumer-record","null-check","constructor"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}