{"record":{"id":"b3ce984d1434e943","repo":"TheAlgorithms/Java","slug":"queue-is-empty-b3ce98","errorCode":null,"errorMessage":"Queue is empty","messagePattern":"Queue is empty","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java","lineNumber":73,"sourceCode":"\n        if (isEmpty()) {\n            front = newNode;\n        } else {\n            rear.next = newNode;\n        }\n        rear = newNode;\n        size++;\n    }\n\n    /**\n     * Removes and returns the element at the front of the queue.\n     *\n     * @return the element at the front of the queue.\n     * @throws NoSuchElementException if the queue is empty.\n     */\n    public T dequeue() {\n        if (isEmpty()) {\n            throw new NoSuchElementException(\"Queue is empty\");\n        }\n\n        T retValue = front.data;\n        front = front.next;\n        size--;\n\n        if (isEmpty()) {\n            rear = null;\n        }\n\n        return retValue;\n    }\n\n    /**\n     * Returns the element at the front of the queue without removing it.\n     *\n     * @return the element at the front of the queue.\n     * @throws NoSuchElementException if the queue is empty.","sourceCodeStart":55,"sourceCodeEnd":91,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/LinkedQueue.java#L55-L91","documentation":"Thrown by LinkedQueue.dequeue() as a NoSuchElementException when isEmpty() (size == 0). The linked queue cannot return a front node that does not exist, so it rejects the removal. This mirrors the standard FIFO underflow contract.","triggerScenarios":"Calling dequeue() before any enqueue(), or calling dequeue() more times than enqueue(). Any removal past the last element throws.","commonSituations":"Consumer thread outpacing the producer; loop counters off by one; draining a queue after work completion with an extra pop; BFS/pipeline stages that dequeue when no input arrived.","solutions":["Guard with isEmpty() before dequeue() and skip/wait when empty.","Use size() in loop bounds so the number of dequeues never exceeds the number of enqueues.","Catch NoSuchElementException when an empty queue is a valid outcome."],"exampleFix":"// before\nT v = queue.dequeue(); // throws when empty\n\n// after\nT v = null;\nif (!queue.isEmpty()) {\n    v = queue.dequeue();\n}","handlingStrategy":"validation","validationCode":"if (!queue.isEmpty()) {\n    T v = queue.dequeue();\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["Guard every dequeue with isEmpty().","Keep enqueue/dequeue counts balanced in producer-consumer code.","Return Optional from your own dequeue wrapper to force callers to handle emptiness."],"tags":["queue","data-structure","underflow","empty-state","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}