TheAlgorithms/Java · error · IllegalArgumentException

imageWidth should be greater than zero

Error message

imageWidth should be greater than zero

What it means

Thrown by KochSnowflake.getKochSnowflake when imageWidth <= 0. The renderer computes geometry (offsets, vector coordinates) derived from imageWidth, so a non-positive width would produce a degenerate/empty image and divide-by-zero-style artifacts; the guard rejects it up front.

Source

Thrown at src/main/java/com/thealgorithms/others/KochSnowflake.java:102

    public static ArrayList<Vector2> iterate(ArrayList<Vector2> initialVectors, int steps) {
        ArrayList<Vector2> vectors = initialVectors;
        for (int i = 0; i < steps; i++) {
            vectors = iterationStep(vectors);
        }

        return vectors;
    }

    /**
     * Method to render the Koch snowflake to a image.
     *
     * @param imageWidth The width of the rendered image.
     * @param steps The number of iterations.
     * @return The image of the rendered Koch snowflake.
     */
    public static BufferedImage getKochSnowflake(int imageWidth, int steps) {
        if (imageWidth <= 0) {
            throw new IllegalArgumentException("imageWidth should be greater than zero");
        }

        double offsetX = imageWidth / 10.;
        double offsetY = imageWidth / 3.7;
        Vector2 vector1 = new Vector2(offsetX, offsetY);
        Vector2 vector2 = new Vector2(imageWidth / 2.0, Math.sin(Math.PI / 3.0) * imageWidth * 0.8 + offsetY);
        Vector2 vector3 = new Vector2(imageWidth - offsetX, offsetY);
        ArrayList<Vector2> initialVectors = new ArrayList<Vector2>();
        initialVectors.add(vector1);
        initialVectors.add(vector2);
        initialVectors.add(vector3);
        initialVectors.add(vector1);
        ArrayList<Vector2> vectors = iterate(initialVectors, steps);
        return getImage(vectors, imageWidth, imageWidth);
    }

    /**
     * Loops through each pair of adjacent vectors. Each line between two

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Supply a positive imageWidth (e.g. 256 or 512) before calling.
  2. Default the width to a sensible positive value when the source config is missing/zero.
  3. Validate the parameter at the boundary (config load / CLI parse) and fail there with context.
  4. Ensure the UI control never offers 0 as a selectable size.

Example fix

// before
BufferedImage img = KochSnowflake.getKochSnowflake(configuredWidth, steps); // configuredWidth may be 0

// after
int width = configuredWidth > 0 ? configuredWidth : 512;
BufferedImage img = KochSnowflake.getKochSnowflake(width, steps);
Defensive patterns

Strategy: validation

Validate before calling

if (imageWidth <= 0) throw new IllegalArgumentException("imageWidth must be > 0; got " + imageWidth);
BufferedImage img = KochSnowflake.getKochSnowflake(imageWidth, steps);

Type guard

public static boolean isPositiveDimension(int dim) {
    return dim > 0;
}

Try / catch

try {
    img = KochSnowflake.getKochSnowflake(width, steps);
} catch (IllegalArgumentException e) {
    img = KochSnowflake.getKochSnowflake(512, steps); // sane default
}

Prevention

When it happens

Trigger: Calling getKochSnowflake(imageWidth, steps) with imageWidth of 0 or a negative number; imageWidth read from a config/CLI argument that defaults to 0 when unset.

Common situations: Unset configuration parameter resolving to 0; CLI parsing returning 0 on missing flag; dimension coming from a UI control whose value is 0 before layout; negative width from a subtraction gone wrong.

Related errors


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