{"record":{"id":"26a048a1f8611b78","repo":"TheAlgorithms/Java","slug":"stack-size-must-be-greater-than-0","errorCode":null,"errorMessage":"Stack size must be greater than 0","messagePattern":"Stack size must be greater than 0","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java","lineNumber":39,"sourceCode":"\n    /**\n     * Creates a stack with a default capacity.\n     */\n    @SuppressWarnings(\"unchecked\")\n    public StackArray() {\n        this(DEFAULT_CAPACITY);\n    }\n\n    /**\n     * Creates a stack with a specified initial capacity.\n     *\n     * @param size the initial capacity of the stack, must be greater than 0\n     * @throws IllegalArgumentException if size is less than or equal to 0\n     */\n    @SuppressWarnings(\"unchecked\")\n    public StackArray(int size) {\n        if (size <= 0) {\n            throw new IllegalArgumentException(\"Stack size must be greater than 0\");\n        }\n        this.maxSize = size;\n        this.stackArray = (T[]) new Object[size];\n        this.top = -1;\n    }\n\n    /**\n     * Pushes an element onto the top of the stack. Resizes the stack if it is full.\n     *\n     * @param value the element to push\n     */\n    @Override\n    public void push(T value) {\n        if (isFull()) {\n            resize(maxSize * 2);\n        }\n        stackArray[++top] = value;\n    }","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/stacks/StackArray.java#L21-L57","documentation":"Thrown by the StackArray(int size) constructor when size is zero or negative. The constructor allocates an Object[] of the given length and sets top=-1; a non-positive size would create an invalid (zero-length or failing) backing array. The IllegalArgumentException rejects this at construction time rather than failing later on push.","triggerScenarios":"Constructing StackArray with a literal 0 or negative number. Passing a capacity derived from user input or config without clamping to a minimum. Passing a computed size (e.g., collection.size() on an empty collection) that evaluates to 0.","commonSituations":"Configuration files where a capacity property is left blank or set to 0. Capacity derived from request payload sizes that can legitimately be zero. Default-value bugs where a constant is mis-set.","solutions":["Pass a strictly positive capacity; use the no-arg constructor if you want the DEFAULT_CAPACITY.","Clamp computed sizes to at least 1 before constructing: Math.max(1, computedSize).","Validate external input (config/UI/API) before it reaches the constructor.","Prefer StackArray() (default capacity) when you have no specific size requirement."],"exampleFix":"// before\nStackArray<String> s = new StackArray<>(inputCapacity);\n// after\nint cap = Math.max(1, inputCapacity);\nStackArray<String> s = new StackArray<>(cap);","handlingStrategy":"validation","validationCode":"int cap = Math.max(1, requestedSize);\nStackArray<T> s = new StackArray<>(cap);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp externally-sourced capacities to a positive minimum.","Prefer the no-arg constructor when default capacity is acceptable.","Validate numeric config before it reaches constructors."],"tags":["stack","invalid-argument","java","datastructures","constructor"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}