{"id":"baf3c7652fd7af1d","repo":"apache/kafka","slug":"partitions-collection-cannot-be-null-baf3c7","errorCode":null,"errorMessage":"Partitions collection cannot be null","messagePattern":"Partitions collection cannot be null","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java","lineNumber":842,"sourceCode":"            } else {\n                log.info(\"Seeking to offset {} for partition {}\", offset, partition);\n            }\n            Metadata.LeaderAndEpoch currentLeaderAndEpoch = this.metadata.currentLeader(partition);\n            SubscriptionState.FetchPosition newPosition = new SubscriptionState.FetchPosition(\n                    offsetAndMetadata.offset(),\n                    offsetAndMetadata.leaderEpoch(),\n                    currentLeaderAndEpoch);\n            this.updateLastSeenEpochIfNewer(partition, offsetAndMetadata);\n            this.subscriptions.seekUnvalidated(partition, newPosition);\n        } finally {\n            release();\n        }\n    }\n\n    @Override\n    public void seekToBeginning(Collection<TopicPartition> partitions) {\n        if (partitions == null)\n            throw new IllegalArgumentException(\"Partitions collection cannot be null\");\n\n        acquireAndEnsureOpen();\n        try {\n            Collection<TopicPartition> parts = partitions.isEmpty() ? this.subscriptions.assignedPartitions() : partitions;\n            subscriptions.requestOffsetReset(parts, AutoOffsetResetStrategy.EARLIEST);\n        } finally {\n            release();\n        }\n    }\n\n    @Override\n    public void seekToEnd(Collection<TopicPartition> partitions) {\n        if (partitions == null)\n            throw new IllegalArgumentException(\"Partitions collection cannot be null\");\n\n        acquireAndEnsureOpen();\n        try {\n            Collection<TopicPartition> parts = partitions.isEmpty() ? this.subscriptions.assignedPartitions() : partitions;","sourceCodeStart":824,"sourceCodeEnd":860,"githubUrl":"https://github.com/apache/kafka/blob/c31c9215e131f8c17e79f8901b48c13ee6aa8e7a/clients/src/main/java/org/apache/kafka/clients/consumer/internals/ClassicKafkaConsumer.java#L824-L860","documentation":"Thrown by seekToBeginning(Collection<TopicPartition>) when the partitions argument is null. The operation accepts an empty collection (treated as 'all assigned partitions') but not null, since null indicates a programming error. The guard runs before acquiring the consumer lock so the failure is immediate.","triggerScenarios":"Calling consumer.seekToBeginning(null). Passing a partition list field that was not initialized or returned null from a resolver.","commonSituations":"Helper that returns null instead of Collections.emptyList() when no partitions need reset. Refactor leaving a field null on a restart path. Tests stubbing partition assignment with null.","solutions":["Pass an explicit collection, using Collections.emptyList() to mean 'reset all assigned partitions'.","Make resolver helpers return Collections.emptyList() instead of null when there is nothing to reset.","Null-check in caller code to surface the misconfiguration with application context."],"exampleFix":"// before\nCollection<TopicPartition> toReset = partitionsToReset(); // returns null\nconsumer.seekToBeginning(toReset);\n\n// after\nCollection<TopicPartition> toReset = partitionsToReset();\nif (toReset == null) toReset = Collections.emptyList();\nconsumer.seekToBeginning(toReset);","handlingStrategy":"validation","validationCode":"java.util.Collection<org.apache.kafka.common.TopicPartition> partitions = /* ... */;\nif (partitions == null) {\n    throw new IllegalArgumentException(\"Partitions collection cannot be null\");\n}\nconsumer.seekToBeginning(partitions); // also applies to seekToEnd","typeGuard":"java.util.Objects.requireNonNull(partitions, \"Partitions collection cannot be null\");","tryCatchPattern":"try {\n    consumer.seekToBeginning(partitions);\n} catch (IllegalArgumentException e) {\n    if (e.getMessage().contains(\"null\")) { consumer.seekToBeginning(java.util.List.of()); }\n    else throw e;\n}","preventionTips":["Pass Collections.emptyList() (which resets all assigned partitions) instead of null","The same null-check applies to seekToEnd since both share the guard","Centralize seekToBeginning/seekToEnd calls behind a non-null precondition helper"],"tags":["consumer","seek","null-argument","validation"],"analyzedSha":"c31c9215e131f8c17e79f8901b48c13ee6aa8e7a","analyzedAt":"2026-08-03T12:34:05.770Z","schemaVersion":2}