TheAlgorithms/Java · error · IllegalArgumentException

Buffer size must be positive

Error message

Buffer size must be positive

What it means

Thrown by the CircularBuffer constructor when size <= 0. The buffer allocates a fixed Object array of the given size, so zero or negative sizes are structurally invalid — there would be no slots to store items and the modulo-based pointer logic would divide by zero.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/buffers/CircularBuffer.java:28

 *
 * @param <Item> The type of elements stored in the circular buffer.
 */
@SuppressWarnings("unchecked")
public class CircularBuffer<Item> {
    private final Item[] buffer;
    private final CircularPointer putPointer;
    private final CircularPointer getPointer;
    private final AtomicInteger size = new AtomicInteger(0);

    /**
     * Constructor to initialize the circular buffer with a specified size.
     *
     * @param size The size of the circular buffer.
     * @throws IllegalArgumentException if the size is zero or negative.
     */
    public CircularBuffer(int size) {
        if (size <= 0) {
            throw new IllegalArgumentException("Buffer size must be positive");
        }
        // noinspection unchecked
        this.buffer = (Item[]) new Object[size];
        this.putPointer = new CircularPointer(0, size);
        this.getPointer = new CircularPointer(0, size);
    }

    /**
     * Checks if the circular buffer is empty.
     * This method is based on the current size of the buffer.
     *
     * @return {@code true} if the buffer is empty, {@code false} otherwise.
     */
    public boolean isEmpty() {
        return size.get() == 0;
    }

    /**

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Validate and clamp the size to a positive minimum before construction.
  2. Provide a sensible default capacity when config is absent.
  3. Fail fast at config load with a clear error naming the required property.

Example fix

// before
new CircularBuffer<>(config.getBufferSize()); // returns 0 if unset
// after
int size = config.getBufferSize();
if (size <= 0) {
    throw new IllegalArgumentException("buffer size must be positive, got " + size);
}
new CircularBuffer<>(size);
Defensive patterns

Strategy: validation

Validate before calling

static CircularBuffer<T> createBuffer(int size) {
    if (size <= 0) throw new IllegalArgumentException("buffer size must be positive, got " + size);
    return new CircularBuffer<>(size);
}

Try / catch

try {
    return new CircularBuffer<>(capacity);
} catch (IllegalArgumentException e) {
    throw new ConfigException("CircularBuffer requires size > 0; got " + capacity);
}

Prevention

When it happens

Trigger: new CircularBuffer<>(0), new CircularBuffer<>(-5). Often the size comes from a config value, a computed capacity, or a user-provided parameter that was left unset (defaulting to 0) or misparsed.

Common situations: Configuration with missing/zeroed buffer capacity; computed sizes that underflow for small workloads; reading capacity from a properties file where the key was absent; passing a primitive default (0) from an uninitialized field.

Related errors


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