{"record":{"id":"276dc4986f16d197","repo":"TheAlgorithms/Java","slug":"cannot-pop-from-an-empty-stack","errorCode":null,"errorMessage":"Cannot pop from an empty stack.","messagePattern":"Cannot pop from an empty stack\\.","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java","lineNumber":60,"sourceCode":"     *\n     * @param item the item to be pushed onto the stack\n     */\n    public void push(Item item) {\n        Node newNode = new Node(item);\n        newNode.previous = head;\n        head = newNode;\n        size++;\n    }\n\n    /**\n     * Removes and returns the item at the top of the stack.\n     *\n     * @return the item at the top of the stack, or {@code null} if the stack is empty\n     * @throws IllegalStateException if the stack is empty\n     */\n    public Item pop() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Cannot pop from an empty stack.\");\n        }\n        Item data = head.data;\n        head = head.previous;\n        size--;\n        return data;\n    }\n\n    /**\n     * Returns the item at the top of the stack without removing it.\n     *\n     * @return the item at the top of the stack, or {@code null} if the stack is empty\n     * @throws IllegalStateException if the stack is empty\n     */\n    public Item peek() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Cannot peek from an empty stack.\");\n        }\n        return head.data;","sourceCodeStart":42,"sourceCodeEnd":78,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/stacks/NodeStack.java#L42-L78","documentation":"Thrown by NodeStack.pop() when the stack contains no elements. NodeStack is a generic linked-list-backed stack; pop() dereferences head.data to retrieve the top item, so calling it on an empty stack would produce a NullPointerException without this guard. The IllegalStateException signals a logic error in the caller — popping more times than you pushed.","triggerScenarios":"Calling pop() on a freshly constructed NodeStack (head==null). Calling pop() more times than push() was called. Calling pop() after a prior pop() already emptied the stack.","commonSituations":"Unbalanced push/pop in expression-evaluation or DFS traversal code. Popping in a loop without an isEmpty() check. Reusing a stack object across multiple operations without resetting or checking its state.","solutions":["Guard every pop() call with an isEmpty() check before invoking it.","Track the expected number of elements externally and only pop when count > 0.","If pop() legitimately may find an empty stack, catch IllegalStateException at the call site.","Audit the calling code for unbalanced push/pop logic."],"exampleFix":"// before\nwhile (true) {\n    Item x = stack.pop();\n    process(x);\n}\n// after\nwhile (!stack.isEmpty()) {\n    Item x = stack.pop();\n    process(x);\n}","handlingStrategy":"validation","validationCode":"if (!stack.isEmpty()) {\n    Item top = stack.pop();\n} else {\n    // handle empty case\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always pair pop() with an isEmpty() check or a while(!isEmpty()) loop.","Track push/pop balance in stateful algorithms.","Prefer isEmpty()-drained loops over fixed-count loops."],"tags":["stack","empty-state","java","datastructures"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}