{"record":{"id":"18571a5e5233aaf0","repo":"TheAlgorithms/Java","slug":"deque-is-empty","errorCode":null,"errorMessage":"Deque is empty","messagePattern":"Deque is empty","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/queues/Deque.java","lineNumber":79,"sourceCode":"            head = newNode;\n            tail = newNode;\n        } else {\n            newNode.prev = tail;\n            tail.next = newNode;\n            tail = newNode;\n        }\n        size++;\n    }\n\n    /**\n     * Removes and returns the first (head) value in the deque\n     *\n     * @return the value of the head of the deque\n     * @throws NoSuchElementException if the deque is empty\n     */\n    public T pollFirst() {\n        if (head == null) {\n            throw new NoSuchElementException(\"Deque is empty\");\n        }\n\n        T oldHeadVal = head.val;\n        if (head == tail) {\n            head = null;\n            tail = null;\n        } else {\n            head = head.next;\n            head.prev = null;\n        }\n        size--;\n        return oldHeadVal;\n    }\n\n    /**\n     * Removes and returns the last (tail) value in the deque\n     *\n     * @return the value of the tail of the deque","sourceCodeStart":61,"sourceCodeEnd":97,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/queues/Deque.java#L61-L97","documentation":"Thrown by Deque.pollFirst() as a NoSuchElementException when head == null, i.e. the doubly-linked deque holds no nodes. Unlike java.util.Deque.pollFirst (which returns null on empty), this implementation throws, so callers cannot rely on the standard-library convention of a null return.","triggerScenarios":"Calling pollFirst() on a newly constructed Deque, or calling pollFirst() until size reaches zero and then calling it once more. Any removal beyond the node count triggers it.","commonSituations":"Assuming this Deque behaves like java.util.ArrayDeque/LinkedList whose poll methods return null on empty; draining a deque in a while loop without an emptiness condition; using pollFirst as the sole loop-exit signal.","solutions":["Check the public size field or isEmpty()/head==null state before calling pollFirst().","Loop while deque.size > 0 (or a custom isEmpty) instead of relying on a null sentinel.","Catch NoSuchElementException when an empty deque is a legitimate control-flow outcome."],"exampleFix":"// before\nT v = deque.pollFirst(); // throws, NOT null like java.util.Deque\n\n// after\nT v = (deque.size > 0) ? deque.pollFirst() : null;","handlingStrategy":"validation","validationCode":"T v = (deque.size > 0) ? deque.pollFirst() : null;","typeGuard":"// narrow: distinguish this throwing Deque from the JDK Deque whose poll returns null\nboolean canPollFirst = deque.size > 0;","tryCatchPattern":"null","preventionTips":["Do NOT assume this Deque.pollFirst returns null on empty like java.util.Deque — it throws.","Loop while size > 0 rather than relying on a null sentinel from poll.","Wrap this deque behind an adapter that returns Optional for poll operations."],"tags":["deque","data-structure","underflow","empty-state","api-convention","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}