{"record":{"id":"712c71c4a4482c71","repo":"TheAlgorithms/Java","slug":"stack-is-empty","errorCode":null,"errorMessage":"Stack is empty","messagePattern":"Stack is empty","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/stacks/GreatestElementConstantTime.java","lineNumber":54,"sourceCode":"            return;\n        }\n\n        mainStack.push(data);\n        if (data > maxStack.peek()) {\n            maxStack.push(data);\n        }\n    }\n\n    /**\n     * Pops an element from the stack.\n     * Checks if the element to be popped is the maximum or not\n     * If so, then pop from the minStack\n     *\n     * @throws NoSuchElementException if the stack is empty.\n     */\n    public void pop() {\n        if (mainStack.isEmpty()) {\n            throw new NoSuchElementException(\"Stack is empty\");\n        }\n\n        int ele = mainStack.pop();\n        if (ele == maxStack.peek()) {\n            maxStack.pop();\n        }\n    }\n\n    /**\n     * Returns the maximum element present in the stack\n     *\n     * @return The element at the top of the maxStack, or null if the stack is empty.\n     */\n    public Integer getMaximumElement() {\n        if (maxStack.isEmpty()) {\n            return null;\n        }\n        return maxStack.peek();","sourceCodeStart":36,"sourceCodeEnd":72,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/stacks/GreatestElementConstantTime.java#L36-L72","documentation":"GreatestElementConstantTime maintains a main stack plus an auxiliary maxStack to report the maximum in O(1). pop() removes the top of mainStack and, if that element equals the current maximum, also pops maxStack. Calling pop() when the stack is empty would underflow both stacks, so it throws java.util.NoSuchElementException with message 'Stack is empty'.","triggerScenarios":"Calling `.pop()` more times than elements were pushed, or calling pop() on a freshly-constructed (empty) instance. Note: getMaximum on an empty stack returns null rather than throwing, so the asymmetry can surprise callers.","commonSituations":"Mismatched push/pop counts in a loop; draining logic that pops in a while-true without an isEmpty guard; popping during error/cleanup paths that run even when nothing was pushed; concurrent/recursive code that pops past its push depth.","solutions":["Guard every pop() with an isEmpty()/size check.","Track the count of pushed elements and never pop beyond it.","Catch NoSuchElementException at call sites where emptiness is an expected runtime condition."],"exampleFix":"// before\nwhile (!stack.getMaximum().equals(target)) {\n    stack.pop(); // throws when drained\n}\n\n// after\nwhile (!stack.isEmpty() && !stack.getMaximum().equals(target)) {\n    stack.pop();\n}","handlingStrategy":"validation","validationCode":"static void safePop(GreatestElementConstantTime s) {\n    if (s.isEmpty()) {            // requires an isEmpty()/size() accessor\n        throw new IllegalStateException(\"cannot pop: stack is empty\");\n    }\n    s.pop();\n}","typeGuard":null,"tryCatchPattern":"try {\n    stack.pop();\n} catch (java.util.NoSuchElementException e) {\n    // expected when the stack was already drained; recover or ignore\n}","preventionTips":["Pair every pop() in a loop with an isEmpty()/size() guard.","Keep an explicit count of pushes and never pop below zero depth.","Remember getMaximum() returns null on empty while pop() throws — handle them differently."],"tags":["stacks","empty-stack","validation","nosuchelement","underflow"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}