{"record":{"id":"0c44a16d34f8ba4b","repo":"krahets/hello-algo","slug":"error-0c44a1","errorCode":null,"errorMessage":"スタックが空","messagePattern":"スタックが空","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"ja/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js","lineNumber":46,"sourceCode":"    /* プッシュ */\n    push(num) {\n        const node = new ListNode(num);\n        node.next = this.#stackPeek;\n        this.#stackPeek = node;\n        this.#stkSize++;\n    }\n\n    /* ポップ */\n    pop() {\n        const num = this.peek();\n        this.#stackPeek = this.#stackPeek.next;\n        this.#stkSize--;\n        return num;\n    }\n\n    /* スタックトップの要素にアクセス */\n    peek() {\n        if (!this.#stackPeek) throw new Error('スタックが空');\n        return this.#stackPeek.val;\n    }\n\n    /* 連結リストを Array に変換して返す */\n    toArray() {\n        let node = this.#stackPeek;\n        const res = new Array(this.size);\n        for (let i = res.length - 1; i >= 0; i--) {\n            res[i] = node.val;\n            node = node.next;\n        }\n        return res;\n    }\n}\n\n/* Driver Code */\n/* スタックを初期化 */\nconst stack = new LinkedListStack();","sourceCodeStart":28,"sourceCodeEnd":64,"githubUrl":"https://github.com/krahets/hello-algo/blob/69932aed1891a7b7f6a0de88cd116d3fe13e7032/ja/codes/javascript/chapter_stack_and_queue/linkedlist_stack.js#L28-L64","documentation":"Thrown by the linked-list stack's peek when the stack pointer (#stackPeek) is null/undefined, i.e. the stack is empty. peek reads #stackPeek.val; the guard converts what would be a TypeError (reading .val of null) into a clear domain error. pop() calls peek() first, so popping an empty stack surfaces the same message.","triggerScenarios":"Calling peek() or pop() on an empty stack (no pushes, or fully popped); calling after #stackPeek was reset to null.","commonSituations":"Unbalanced push/pop in algorithms; peeking before the first push; recursion-simulation stacks that drain.","solutions":["Check !stack.isEmpty() (or #stackPeek presence via an exposed method) before peek()/pop().","Use while (!stack.isEmpty()) for drain loops.","Return a sentinel when empty instead of throwing.","Keep push/pop counts balanced."],"exampleFix":"// before\nconst top = stack.peek();  // throws if empty\n\n// after\nconst top = stack.isEmpty() ? null : stack.peek();","handlingStrategy":"validation","validationCode":"function safePeek(stack) {\n  return stack.isEmpty() ? null : stack.peek();\n}","typeGuard":"const isNonEmpty = (s) => typeof s.isEmpty === 'function' && !s.isEmpty();","tryCatchPattern":"try {\n  return stack.peek();\n} catch (e) {\n  if (e instanceof Error && e.message === 'スタックが空') return null;\n  throw e;\n}","preventionTips":["Check isEmpty() before peek()/pop().","Keep push/pop counts balanced.","Return a sentinel for empty peeks."],"tags":["stack","linked-list","javascript","empty-state"],"backgroundTag":null,"analyzedSha":"69932aed1891a7b7f6a0de88cd116d3fe13e7032","analyzedAt":"2026-08-13T23:02:37.581Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}