{"record":{"id":"78ab4c100fc923ad","repo":"TheAlgorithms/Java","slug":"empty-stack-nothing-to-pop","errorCode":null,"errorMessage":"Empty stack. Nothing to pop","messagePattern":"Empty stack\\. Nothing to pop","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/stacks/StackOfLinkedList.java","lineNumber":76,"sourceCode":"     * @return <tt>true</tt> if the element is added successfully\n     */\n    public boolean push(int x) {\n        Node newNode = new Node(x);\n        newNode.next = head;\n        head = newNode;\n        size++;\n        return true;\n    }\n\n    /**\n     * Removes and returns the top element of the stack.\n     *\n     * @return the element at the top of the stack\n     * @throws NoSuchElementException if the stack is empty\n     */\n    public int pop() {\n        if (size == 0) {\n            throw new NoSuchElementException(\"Empty stack. Nothing to pop\");\n        }\n        Node destroy = head;\n        head = head.next;\n        int retValue = destroy.data;\n        destroy = null; // Help garbage collection\n        size--;\n        return retValue;\n    }\n\n    /**\n     * Returns the top element of the stack without removing it.\n     *\n     * @return the element at the top of the stack\n     * @throws NoSuchElementException if the stack is empty\n     */\n    public int peek() {\n        if (size == 0) {\n            throw new NoSuchElementException(\"Empty stack. Nothing to peek\");","sourceCodeStart":58,"sourceCodeEnd":94,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/stacks/StackOfLinkedList.java#L58-L94","documentation":"Thrown by StackOfLinkedList.pop() when the stack's size is 0. pop() dereferences head to read head.data and advance to head.next, so on an empty stack it would throw NullPointerException. The NoSuchElementException signals that there is no element to remove — a caller logic error.","triggerScenarios":"Calling pop() on a newly constructed StackOfLinkedList (size==0). Calling pop() after the stack has been emptied. Popping in a loop without checking size.","commonSituations":"Unbalanced push/pop in graph traversals or expression evaluation. Reusing a stack instance without state verification. Off-by-one loop bounds.","solutions":["Check size==0 (or use a provided isEmpty() method) before pop().","Iterate with while (stack.size() > 0) rather than a fixed count.","Catch NoSuchElementException if draining past empty is an expected branch.","Audit for unbalanced push/pop at the call sites."],"exampleFix":"// before\nwhile (count-- > 0) {\n    int v = stack.pop();\n}\n// after\nwhile (stack.size() > 0) {\n    int v = stack.pop();\n}","handlingStrategy":"validation","validationCode":"while (stack.size() > 0) {\n    int v = stack.pop();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check size()==0 before pop().","Use size()-drained loops over fixed counts.","Audit for unbalanced push/pop at call sites."],"tags":["stack","empty-state","java","datastructures","linked-list"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}