{"record":{"id":"a5efa1294935fdb8","repo":"TheAlgorithms/Java","slug":"stack-is-empty-cannot-peek-element","errorCode":null,"errorMessage":"Stack is empty, cannot peek element","messagePattern":"Stack is empty, cannot peek element","errorType":"exception","errorClass":"IllegalStateException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java","lineNumber":87,"sourceCode":"            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\");\n        }\n        return stackArray[top];\n    }\n\n    /**\n     * Resizes the internal array to a new capacity.\n     *\n     * @param newSize the new size of the stack array\n     */\n    private void resize(int newSize) {\n        @SuppressWarnings(\"unchecked\") T[] newArray = (T[]) new Object[newSize];\n        System.arraycopy(stackArray, 0, newArray, 0, top + 1);\n        stackArray = newArray;\n        maxSize = newSize;\n    }\n\n    /**\n     * Checks if the stack is full.","sourceCodeStart":69,"sourceCodeEnd":105,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java#L69-L105","documentation":"Thrown by StackArray.peek() when the stack is empty. peek() returns stackArray[top]; with top==-1 this would index out of bounds. The IllegalStateException enforces the precondition that a top element must exist before inspection.","triggerScenarios":"Calling peek() on a new StackArray. Calling peek() after the stack has been fully drained. Inspecting the top before the first push in an algorithm.","commonSituations":"Monotonic-stack or parsing algorithms that peek before seeding. Stateful processing where the first iteration peeks an uninitialized stack. Loops with off-by-one bounds.","solutions":["Guard every peek() with isEmpty().","Push a sentinel element so the stack is never empty during the peek window.","Terminate the consuming loop on isEmpty() rather than an external counter.","Catch IllegalStateException if empty-peek is a valid control-flow branch."],"exampleFix":"// before\nT top = stack.peek();\n// after\nT top = stack.isEmpty() ? null : stack.peek();","handlingStrategy":"validation","validationCode":"T top = stack.isEmpty() ? null : stack.peek();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check isEmpty() before peek().","Push a sentinel so the stack is never empty during peek windows.","Terminate loops on the empty condition."],"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"}