{"id":"6aabc712626f61a3","repo":"apache/kafka","slug":"topic-partition-collection-to-assign-to-cannot-be","errorCode":null,"errorMessage":"Topic partition collection to assign to cannot be null","messagePattern":"Topic partition collection to assign to cannot be null","errorType":"validation","errorClass":"java.lang.IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java","lineNumber":612,"sourceCode":"        try {\n            fetcher.clearBufferedDataForUnassignedPartitions(Collections.emptySet());\n            if (this.coordinator != null) {\n                this.coordinator.onLeavePrepare();\n                this.coordinator.maybeLeaveGroup(CloseOptions.GroupMembershipOperation.DEFAULT, \"the consumer unsubscribed from all topics\");\n            }\n            this.subscriptions.unsubscribe();\n            log.info(\"Unsubscribed all topics or patterns and assigned partitions\");\n        } finally {\n            release();\n        }\n    }\n\n    @Override\n    public void assign(Collection<TopicPartition> partitions) {\n        acquireAndEnsureOpen();\n        try {\n            if (partitions == null) {\n                throw new IllegalArgumentException(\"Topic partition collection to assign to cannot be null\");\n            } else if (partitions.isEmpty()) {\n                this.unsubscribe();\n            } else {\n                for (TopicPartition tp : partitions) {\n                    String topic = (tp != null) ? tp.topic() : null;\n                    if (isBlank(topic))\n                        throw new IllegalArgumentException(\"Topic partitions to assign to cannot have null or empty topic\");\n                }\n                fetcher.clearBufferedDataForUnassignedPartitions(partitions);\n\n                // make sure the offsets of topic partitions the consumer is unsubscribing from\n                // are committed since there will be no following rebalance\n                if (coordinator != null)\n                    this.coordinator.maybeAutoCommitOffsetsAsync(time.milliseconds());\n\n                log.info(\"Assigned to partition(s): {}\", partitions.stream().map(TopicPartition::toString).collect(Collectors.joining(\", \")));\n                if (this.subscriptions.assignFromUser(new HashSet<>(partitions)))\n                    metadata.requestUpdateForNewTopics();","sourceCodeStart":594,"sourceCodeEnd":630,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L594-L630","documentation":"Thrown by assign(Collection<TopicPartition>) when the partitions argument is null. Manual assignment (the non-group path) requires an explicit, non-null collection; null indicates a programming error rather than an intentional unsubscribe. The guard fires inside the acquired lock before any subscription state changes.","triggerScenarios":"Calling consumer.assign(null). Passing a List<TopicPartition> field that was not populated or was set to null by a helper method.","commonSituations":"Refactor leaving an assignment list uninitialized. Conditional logic that returns null instead of an empty list. Tests stubbing a partition resolver that returns null.","solutions":["Pass an explicit collection of TopicPartition objects, even if it is Collections.emptyList() (an empty list is treated as unsubscribe, not an error).","Return Collections.emptyList() from helper methods instead of null when no partitions are resolved.","Add a null check in caller code to surface the misconfiguration earlier with application context."],"exampleFix":"// before\nList<TopicPartition> parts = partitionResolver.resolve(topic); // returns null\nconsumer.assign(parts);\n\n// after\nList<TopicPartition> parts = partitionResolver.resolve(topic);\nif (parts == null) parts = Collections.emptyList();\nconsumer.assign(parts);","handlingStrategy":"validation","validationCode":"java.util.Collection<org.apache.kafka.common.TopicPartition> partitions = /* ... */;\nif (partitions == null) {\n    throw new IllegalArgumentException(\"Topic partition collection to assign to cannot be null\");\n}\nconsumer.assign(partitions);","typeGuard":"java.util.Objects.requireNonNull(partitions, \"Topic partition collection to assign to cannot be null\");","tryCatchPattern":"try {\n    consumer.assign(partitions);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"null\")) { consumer.assign(java.util.List.of()); }\n    else throw e;\n}","preventionTips":["Never pass a possibly-null collection to assign(); pass Collections.emptyList() to clear assignment","Build assignment collections via a helper that guarantees non-null"],"tags":["consumer","assign","null-argument","validation"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}