{"record":{"id":"17c6bc777015dc3f","repo":"TheAlgorithms/Java","slug":"empty-stack-nothing-to-peek","errorCode":null,"errorMessage":"Empty stack. Nothing to peek","messagePattern":"Empty stack\\. Nothing to peek","errorType":"exception","errorClass":"NoSuchElementException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/stacks/StackOfLinkedList.java","lineNumber":94,"sourceCode":"            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\");\n        }\n        return head.data;\n    }\n\n    @Override\n    public String toString() {\n        Node cur = head;\n        StringBuilder builder = new StringBuilder();\n        while (cur != null) {\n            builder.append(cur.data).append(\"->\");\n            cur = cur.next;\n        }\n        return builder.replace(builder.length() - 2, builder.length(), \"\").toString(); // Remove the last \"->\"\n    }\n\n    /**\n     * Checks if the stack is empty.\n     *","sourceCodeStart":76,"sourceCodeEnd":112,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/stacks/StackOfLinkedList.java#L76-L112","documentation":"Thrown by StackOfLinkedList.peek() when the stack holds no elements (size==0). peek() reads head.data, which would NPE on an empty stack. The NoSuchElementException enforces that a top element must exist before it is inspected.","triggerScenarios":"Calling peek() on a fresh StackOfLinkedList. Calling peek() after draining the stack. Inspecting the top in an algorithm before any push has occurred.","commonSituations":"Parsing or matching algorithms that peek before seeding a sentinel. Stateful processors with an uninitialized first iteration. Off-by-one loop termination.","solutions":["Check size==0 before calling peek().","Seed the stack with a sentinel so it is never empty during the peek window.","Terminate loops on the empty condition rather than an external counter.","Catch NoSuchElementException if empty-peek is a legitimate branch."],"exampleFix":"// before\nint top = stack.peek();\n// after\nint top = (stack.size() == 0) ? defaultValue : stack.peek();","handlingStrategy":"validation","validationCode":"int top = (stack.size() == 0) ? defaultValue : stack.peek();","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Guard peek() with a size check.","Seed a sentinel when the algorithm must always peek.","Terminate loops on the empty condition."],"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"}