{"record":{"id":"2a9b054bad11ae51","repo":"TheAlgorithms/Java","slug":"stack-cannot-be-null","errorCode":null,"errorMessage":"Stack cannot be null","messagePattern":"Stack cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java","lineNumber":42,"sourceCode":" * @author Ishika Agarwal, 2021\n */\npublic final class ReverseStack {\n    private ReverseStack() {\n    }\n\n    /**\n     * Reverses the order of elements in the given stack using recursion.\n     * Steps:\n     * 1. Check if the stack is empty. If so, return.\n     * 2. Pop the top element from the stack.\n     * 3. Recursively reverse the remaining stack.\n     * 4. Insert the originally popped element at the bottom of the reversed stack.\n     *\n     * @param stack the stack to reverse; should not be null\n     */\n    public static void reverseStack(Stack<Integer> stack) {\n        if (stack == null) {\n            throw new IllegalArgumentException(\"Stack cannot be null\");\n        }\n        if (stack.isEmpty()) {\n            return;\n        }\n\n        int element = stack.pop();\n        reverseStack(stack);\n        insertAtBottom(stack, element);\n    }\n\n    /**\n     * Inserts the specified element at the bottom of the stack.\n     *\n     * <p>This method is a helper for {@link #reverseStack(Stack)}.\n     *\n     * Steps:\n     * 1. If the stack is empty, push the element and return.\n     * 2. Remove the top element from the stack.","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java#L24-L60","documentation":"Thrown by ReverseStack.reverseStack() when the caller passes a null Stack reference. The method recurses on the stack (pop, reverse, insertAtBottom), so a null reference would cause an immediate NullPointerException. The IllegalArgumentException is a fail-fast precondition check on the single public parameter.","triggerScenarios":"Passing a Stack variable that was declared but never assigned. Passing the result of a factory or lookup method that returned null. Calling reverseStack inside a pipeline where an upstream stage failed silently and propagated null.","commonSituations":"Uninitialized fields in DI/parsing code. Deserialization or config loading that yields null when a section is absent. Optional/map chain that was not given an orElse fallback before the call.","solutions":["Initialize the Stack before passing it: pass new Stack<>() rather than null when you mean 'no elements'.","Add a null check at the call site and skip the reversal or substitute an empty stack.","Trace the source of the stack reference to ensure the producer never returns null.","Use Optional or a non-null annotation to make the null contract explicit upstream."],"exampleFix":"// before\nReverseStack.reverseStack(maybeNullStack);\n// after\nif (maybeNullStack != null) {\n    ReverseStack.reverseStack(maybeNullStack);\n}","handlingStrategy":"validation","validationCode":"if (stack != null) {\n    ReverseStack.reverseStack(stack);\n}","typeGuard":"java.util.Objects.requireNonNull(stack, \"stack\");","tryCatchPattern":null,"preventionTips":["Never pass null for collection arguments; pass empty collections instead.","Fix producers to return non-null defaults.","Use Optional to make nullability explicit."],"tags":["stack","null-argument","java","datastructures","precondition"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}