{"record":{"id":"a58a2b4a7aa576c1","repo":"apache/pulsar","slug":"nosuchelementexception","errorCode":null,"errorMessage":"NoSuchElementException","messagePattern":"NoSuchElementException","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueue.java","lineNumber":79,"sourceCode":"\n    public GrowableArrayBlockingQueue() {\n        this(64);\n    }\n\n    @SuppressWarnings(\"unchecked\")\n    public GrowableArrayBlockingQueue(int initialCapacity) {\n        headIndex.value = 0;\n        tailIndex.value = 0;\n\n        int capacity = io.netty.util.internal.MathUtil.findNextPositivePowerOfTwo(initialCapacity);\n        data = (T[]) new Object[capacity];\n    }\n\n    @Override\n    public T remove() {\n        T item = poll();\n        if (item == null) {\n            throw new NoSuchElementException();\n        }\n\n        return item;\n    }\n\n    @Override\n    public T poll() {\n        return pollIf(v -> true);\n    }\n\n    public T pollIf(Predicate<T> predicate) {\n        headLock.lock();\n        try {\n            if (SIZE_UPDATER.get(this) > 0) {\n                T item = data[headIndex.value];\n                if (!predicate.test(item)) {\n                    return null;\n                }","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/apache/pulsar/blob/820761864ed8e2a7d2e52dd9763ad2ae117c1395/pulsar-common/src/main/java/org/apache/pulsar/common/util/collections/GrowableArrayBlockingQueue.java#L61-L97","documentation":"GrowableArrayBlockingQueue.remove (BlockingQueue contract): the queue was empty at removal time, so there is no element to remove and NoSuchElementException is thrown — the standard j.u.c. behavior for remove() on an empty queue.","triggerScenarios":"Calling remove() on an empty queue (no elements ever added, or all elements already consumed).","commonSituations":"Draining a queue in a loop without checking isEmpty, or race where another consumer took the last element between isEmpty and remove.","solutions":["Check size/emptiness before calling remove, or use poll() which returns null"],"exampleFix":"// before\nT item = queue.remove();\n// after\nT item = queue.poll();\nif (item == null) {\n    return; // queue empty\n}","handlingStrategy":"type-guard","validationCode":"if (!queue.isEmpty()) { T item = queue.remove(); ... }\n// or preferably: T item = queue.poll();","typeGuard":null,"tryCatchPattern":"try {\n    T item = queue.remove();\n} catch (NoSuchElementException e) {\n    // queue was empty\n}","preventionTips":["Prefer poll()/peek() over remove()/element() under concurrency","Never assume isEmpty() then remove() is atomic","Handle empty-queue drains explicitly"],"tags":["java","nosuchelement","empty-collection"],"backgroundTag":"nosuchelement-on-empty","analyzedSha":"820761864ed8e2a7d2e52dd9763ad2ae117c1395","analyzedAt":"2026-09-06T00:14:20.138Z","contentChangedAt":"2026-09-06T00:14:20.138Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}