{"record":{"id":"56abaf38e09892b9","repo":"TheAlgorithms/Java","slug":"queue-is-empty-cannot-remove-element","errorCode":null,"errorMessage":"Queue is empty, cannot remove element","messagePattern":"Queue is empty, cannot remove element","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/Queue.java","lineNumber":69,"sourceCode":"        if (isFull()) {\n            return false;\n        }\n        rear = (rear + 1) % maxSize;\n        queueArray[rear] = element;\n        nItems++;\n        return true;\n    }\n\n    /**\n     * Removes and returns the element from the front of the queue.\n     *\n     * @return The element removed from the front of the queue.\n     * @throws IllegalStateException if the queue is empty.\n     */\n    @SuppressWarnings(\"unchecked\")\n    public T remove() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Queue is empty, cannot remove element\");\n        }\n        T removedElement = (T) queueArray[front];\n        queueArray[front] = null; // Optional: Clear the reference for garbage collection\n        front = (front + 1) % maxSize;\n        nItems--;\n        return removedElement;\n    }\n\n    /**\n     * Checks the element at the front of the queue without removing it.\n     *\n     * @return Element at the front of the queue.\n     * @throws IllegalStateException if the queue is empty.\n     */\n    @SuppressWarnings(\"unchecked\")\n    public T peekFront() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Queue is empty, cannot peek front\");","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/Queue.java#L51-L87","documentation":"Thrown by Queue.remove() as an IllegalStateException when isEmpty() (nItems == 0). The array ring buffer cannot return a front element it does not hold, so removal from an empty queue is rejected. This is the standard FIFO underflow guard.","triggerScenarios":"Calling remove() before any insert(), or calling remove() more times than insert(). Also after the queue is fully drained, any further remove() throws.","commonSituations":"Consumer outpacing producer; loop bounds larger than the number of inserts; draining logic with an extra pop after exhaustion; request handler removing from a shared queue with no pending work.","solutions":["Guard with isEmpty() before remove() and skip or wait when empty.","Drive removal loops by nItems/size rather than an external counter that can overshoot.","Catch IllegalStateException when an empty queue is an expected branch."],"exampleFix":"// before\nT v = queue.remove(); // throws when empty\n\n// after\nT v = null;\nif (!queue.isEmpty()) {\n    v = queue.remove();\n}","handlingStrategy":"validation","validationCode":"if (!queue.isEmpty()) {\n    T v = queue.remove();\n}","typeGuard":"null","tryCatchPattern":"null","preventionTips":["isEmpty()-guard every remove().","Use the queue's own size/empty state to bound drain loops.","Wrap remove to return Optional so callers handle the empty case in the type."],"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"}