{"record":{"id":"777902ab33c0278b","repo":"TheAlgorithms/Java","slug":"stack-is-empty-cannot-pop-element","errorCode":null,"errorMessage":"Stack is empty, cannot pop element","messagePattern":"Stack is empty, cannot pop element","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java","lineNumber":69,"sourceCode":"    @Override\n    public void push(T value) {\n        if (isFull()) {\n            resize(maxSize * 2);\n        }\n        stackArray[++top] = value;\n    }\n\n    /**\n     * Removes and returns the element from the top of the stack. Shrinks the stack if\n     * its size is below a quarter of its capacity, but not below the default capacity.\n     *\n     * @return the element removed from the top of the stack\n     * @throws IllegalStateException if the stack is empty\n     */\n    @Override\n    public T pop() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Stack is empty, cannot pop element\");\n        }\n        T value = stackArray[top--];\n        if (top + 1 < maxSize / 4 && maxSize > DEFAULT_CAPACITY) {\n            resize(maxSize / 2);\n        }\n        return value;\n    }\n\n    /**\n     * Returns the element at the top of the stack without removing it.\n     *\n     * @return the top element of the stack\n     * @throws IllegalStateException if the stack is empty\n     */\n    @Override\n    public T peek() {\n        if (isEmpty()) {\n            throw new IllegalStateException(\"Stack is empty, cannot peek element\");","sourceCodeStart":51,"sourceCodeEnd":87,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java#L51-L87","documentation":"Thrown by StackArray.pop() when the stack holds no elements. pop() reads stackArray[top--], so on an empty stack top is -1 and the index would be invalid. The IllegalStateException guards a precondition violation: removing an element that does not exist.","triggerScenarios":"Calling pop() on a freshly constructed StackArray (top==-1). Calling pop() more times than push(). Popping inside a loop controlled by an external count rather than isEmpty().","commonSituations":"Unbalanced push/pop in evaluation loops. Reusing a stack across operations without verifying state. Off-by-one loop termination causing an extra pop after drain.","solutions":["Check isEmpty() before every pop().","Drive pop loops with while (!isEmpty()) instead of a fixed iteration count.","If an empty pop is an expected branch, catch IllegalStateException and break.","Audit caller logic for unbalanced push/pop counts."],"exampleFix":"// before\nfor (int i = 0; i < n; i++) {\n    T v = stack.pop();\n}\n// after\nwhile (!stack.isEmpty()) {\n    T v = stack.pop();\n}","handlingStrategy":"validation","validationCode":"while (!stack.isEmpty()) {\n    T v = stack.pop();\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Drive pop loops with isEmpty() rather than a counter.","Keep push/pop counts balanced.","Verify stack state before reuse."],"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"}