{"record":{"id":"fda0903711042f33","repo":"apache/kafka","slug":"topic-cannot-be-null","errorCode":null,"errorMessage":"Topic cannot be null","messagePattern":"Topic cannot be null","errorType":"validation","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/996fb4585aa1bcc8980b0e1b8d6b168b986cd979/clients/src/main/java/org/apache/kafka/clients/consumer/ConsumerRecord.java#L135-L171","documentation":"Thrown by the ConsumerRecord constructor when the topic argument is null. A ConsumerRecord models a single record from a specific topic-partition, so a null topic would make the record unusable for partition lookup, offset tracking, and rebalance accounting. The check happens at construction so the bad value cannot propagate.","triggerScenarios":"Constructing a ConsumerRecord directly (typically in tests or in a custom deserializer/interceptor) with topic=null; deserializing from a source that lost its topic field; mocking records without setting topic; an interceptor rewriting records and passing null topic.","commonSituations":"Unit tests building ConsumerRecord fixtures; custom plugins that wrap/replace records; consumers reading from non-Kafka sources being adapted into ConsumerRecord; serializer bugs that drop topic context.","solutions":["Pass a non-null topic string when constructing ConsumerRecord (use the real topic name from the source).","In interceptors/wrappers, propagate the original record.topic() rather than null.","In tests, use a literal topic constant matching the partition you also set."],"exampleFix":"// before\nnew ConsumerRecord<>(null, 0, 0L, key, value);\n\n// after\nnew ConsumerRecord<>(\"orders\", 0, 0L, key, value);","handlingStrategy":"validation","validationCode":"if (topic == null) throw new IllegalArgumentException(\"topic must be non-null when building ConsumerRecord\");\nnew ConsumerRecord<>(topic, partition, offset, key, value, new RecordHeaders());","typeGuard":"static String requireTopic(String t) {\n    return Objects.requireNonNull(t, \"ConsumerRecord topic\");\n}","tryCatchPattern":"try {\n    return new ConsumerRecord<>(topic, partition, offset, key, value, headers);\n} catch (IllegalArgumentException e) {\n    if (\"Topic cannot be null\".equals(e.getMessage())) {\n        // fall back to a safe default topic or rethrow as a domain error\n    }\n    throw e;\n}","preventionTips":["In adapters/interceptors, propagate the source topic explicitly; never null it out.","Centralize ConsumerRecord construction behind a factory that validates topic and headers.","Add a fixture builder in tests that defaults topic to a non-null constant."],"tags":["consumer","consumer-record","null-check","constructor"],"backgroundTag":null,"analyzedSha":"996fb4585aa1bcc8980b0e1b8d6b168b986cd979","analyzedAt":"2026-08-11T22:03:28.655Z","contentChangedAt":null,"schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}