TheAlgorithms/Java · error · IllegalArgumentException

imageWidth should be greater than zero

Error message

imageWidth should be greater than zero

What it means

Thrown by Mandelbrot.getImage when imageWidth <= 0. The method allocates a BufferedImage of that width and divides figureWidth by imageWidth, so a non-positive width would create an invalid image and a division by zero; it is rejected before allocation.

Source

Thrown at src/main/java/com/thealgorithms/others/Mandelbrot.java:77

     * coordinates are used: image-coordinates that refer to the pixels and
     * figure-coordinates that refer to the complex numbers inside and outside
     * the Mandelbrot set. The figure-coordinates in the arguments of this
     * method determine which section of the Mandelbrot set is viewed. The main
     * area of the Mandelbrot set is roughly between "-1.5 < x < 0.5" and "-1 <
     * y < 1" in the figure-coordinates.
     *
     * @param imageWidth The width of the rendered image.
     * @param imageHeight The height of the rendered image.
     * @param figureCenterX The x-coordinate of the center of the figure.
     * @param figureCenterY The y-coordinate of the center of the figure.
     * @param figureWidth The width of the figure.
     * @param maxStep Maximum number of steps to check for divergent behavior.
     * @param useDistanceColorCoding Render in color or black and white.
     * @return The image of the rendered Mandelbrot set.
     */
    public static BufferedImage getImage(int imageWidth, int imageHeight, double figureCenterX, double figureCenterY, double figureWidth, int maxStep, boolean useDistanceColorCoding) {
        if (imageWidth <= 0) {
            throw new IllegalArgumentException("imageWidth should be greater than zero");
        }

        if (imageHeight <= 0) {
            throw new IllegalArgumentException("imageHeight should be greater than zero");
        }

        if (maxStep <= 0) {
            throw new IllegalArgumentException("maxStep should be greater than zero");
        }

        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);
        double figureHeight = figureWidth / imageWidth * imageHeight;

        // loop through the image-coordinates
        for (int imageX = 0; imageX < imageWidth; imageX++) {
            for (int imageY = 0; imageY < imageHeight; imageY++) {
                // determine the figure-coordinates based on the image-coordinates
                double figureX = figureCenterX + ((double) imageX / imageWidth - 0.5) * figureWidth;

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Provide a positive imageWidth (e.g. 800) when calling getImage.
  2. Default to a sensible positive width when the source value is missing or zero.
  3. Validate dimensions at the input boundary and fail with a clear message there.
  4. Ensure any interactive control constrains width to a minimum of 1.

Example fix

// before
BufferedImage img = Mandelbrot.getImage(width, height, cx, cy, fw, maxStep, color); // width may be 0

// after
int w = width > 0 ? width : 800;
BufferedImage img = Mandelbrot.getImage(w, height, cx, cy, fw, maxStep, color);
Defensive patterns

Strategy: validation

Validate before calling

if (imageWidth <= 0) imageWidth = 800;
BufferedImage img = Mandelbrot.getImage(imageWidth, imageHeight, cx, cy, fw, maxStep, color);

Type guard

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

Try / catch

try {
    img = Mandelbrot.getImage(w, h, cx, cy, fw, maxStep, color);
} catch (IllegalArgumentException e) {
    img = Mandelbrot.getImage(800, h, cx, cy, fw, maxStep, color);
}

Prevention

When it happens

Trigger: Calling getImage(imageWidth, ...) with imageWidth of 0 or negative; width sourced from a config/CLI argument that defaults to 0 when unset.

Common situations: Unset dimension parameter defaulting to 0; CLI flag parsing returning 0; UI size control at 0 before layout completes; negative width from an arithmetic error.

Related errors


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