TheAlgorithms/Java · error · IllegalArgumentException

Stack cannot be null

Error message

Stack cannot be null

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/stacks/ReverseStack.java:42

 * @author Ishika Agarwal, 2021
 */
public final class ReverseStack {
    private ReverseStack() {
    }

    /**
     * Reverses the order of elements in the given stack using recursion.
     * Steps:
     * 1. Check if the stack is empty. If so, return.
     * 2. Pop the top element from the stack.
     * 3. Recursively reverse the remaining stack.
     * 4. Insert the originally popped element at the bottom of the reversed stack.
     *
     * @param stack the stack to reverse; should not be null
     */
    public static void reverseStack(Stack<Integer> stack) {
        if (stack == null) {
            throw new IllegalArgumentException("Stack cannot be null");
        }
        if (stack.isEmpty()) {
            return;
        }

        int element = stack.pop();
        reverseStack(stack);
        insertAtBottom(stack, element);
    }

    /**
     * Inserts the specified element at the bottom of the stack.
     *
     * <p>This method is a helper for {@link #reverseStack(Stack)}.
     *
     * Steps:
     * 1. If the stack is empty, push the element and return.
     * 2. Remove the top element from the stack.

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Initialize the Stack before passing it: pass new Stack<>() rather than null when you mean 'no elements'.
  2. Add a null check at the call site and skip the reversal or substitute an empty stack.
  3. Trace the source of the stack reference to ensure the producer never returns null.
  4. Use Optional or a non-null annotation to make the null contract explicit upstream.

Example fix

// before
ReverseStack.reverseStack(maybeNullStack);
// after
if (maybeNullStack != null) {
    ReverseStack.reverseStack(maybeNullStack);
}
Defensive patterns

Strategy: validation

Validate before calling

if (stack != null) {
    ReverseStack.reverseStack(stack);
}

Type guard

java.util.Objects.requireNonNull(stack, "stack");

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/2a9b054bad11ae51. Report an issue: GitHub.