{"id":"c345682f9777ba7e","repo":"apache/kafka","slug":"topic-partitions-collection-to-assign-to-cannot-be","errorCode":null,"errorMessage":"Topic partitions collection to assign to cannot be null","messagePattern":"Topic partitions 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/AsyncKafkaConsumer.java","lineNumber":1891,"sourceCode":"     * been made.\n     * @return The set of topics currently subscribed to\n     */\n    @Override\n    public Set<String> subscription() {\n        acquireAndEnsureOpen();\n        try {\n            return Set.copyOf(subscriptions.subscription());\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 partitions collection to assign to cannot be null\");\n            }\n\n            if (partitions.isEmpty()) {\n                unsubscribe();\n                return;\n            }\n\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\n            // Clear the buffered data which are not a part of newly assigned topics\n            final Set<TopicPartition> currentTopicPartitions = new HashSet<>();\n\n            for (TopicPartition tp : subscriptions.assignedPartitions()) {\n                if (partitions.contains(tp))","sourceCodeStart":1873,"sourceCodeEnd":1909,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/AsyncKafkaConsumer.java#L1873-L1909","documentation":"IllegalArgumentException from assign(Collection<TopicPartition>) when the argument is null. assign is the manual-assignment entry point; null is treated as a programmer error rather than an unsubscribe signal (pass an empty collection to unsubscribe). The guard runs after acquireAndEnsureOpen, so a closed consumer would already have thrown, and before any fetch-buffer mutation.","triggerScenarios":"Calling consumer.assign(null); also reachable via wrappers/frameworks that forward a nullable collection without checking, or by passing the result of a stream/mapping operation that yielded null (rare but possible with custom collectors).","commonSituations":"Static-analysis refactors that produced null sentinels; reflective/generic code that wraps assign() with an Object[]; Kotlin/Java interop where a nullable List<TopicPartition>? is forwarded unguarded; misreading the API contract where empty list means unsubscribe but null is treated as an error.","solutions":["Pass a non-null Collection<TopicPartition>; use Collections.emptySet() (or assign(Collections.emptyList())) if you intend to unsubscribe.","Fix upstream: ensure the collection is built via stream().collect() rather than assigned from a possibly-null source.","Add a null check before assign() to fail with a clearer application-level message if that is more useful than the library's."],"exampleFix":"// before\nCollection<TopicPartition> tps = computePartitions(); // may return null\nconsumer.assign(tps); // throws if null\n\n// after\nCollection<TopicPartition> tps = computePartitions();\nconsumer.assign(tps == null ? Collections.emptyList() : tps);","handlingStrategy":"type-guard","validationCode":"if (partitions == null) {\n    throw new IllegalArgumentException(\"partitions must not be null\");\n}\nconsumer.assign(partitions);","typeGuard":"Collection<TopicPartition> requireNonNullPartitions(Collection<TopicPartition> partitions) {\n    return java.util.Objects.requireNonNull(partitions, \"Topic partitions collection to assign to cannot be null\");\n}","tryCatchPattern":null,"preventionTips":["Null-check (or use Objects.requireNonNull) on the partitions collection before assign(); the API does not default it.","Use Collections.emptyList() to unsubscribe via assign() rather than passing null.","Also validate that no element is null and no TopicPartition has a blank topic, since assign() rejects those too."],"tags":["kafka","consumer","assignment","validation"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}