TheAlgorithms/Java · error · IllegalArgumentException

maxStep should be greater than zero

Error message

maxStep should be greater than zero

What it means

Thrown by Mandelbrot.getImage when maxStep <= 0. maxStep is the iteration cap used to decide whether a point diverges (escapes the Mandelbrot set); zero or negative steps means the escape loop never runs, so the library rejects it to avoid a meaningless all-interior render.

Source

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

     * @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;
                double figureY = figureCenterY + ((double) imageY / imageHeight - 0.5) * figureHeight;

                double distance = getDistance(figureX, figureY, maxStep);

                // color the corresponding pixel based on the selected coloring-function
                image.setRGB(imageX, imageY, useDistanceColorCoding ? colorCodedColorMap(distance).getRGB() : blackAndWhiteColorMap(distance).getRGB());
            }
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a positive maxStep (e.g. 256 or 1000 for finer detail).
  2. Default maxStep to a reasonable positive value when unset.
  3. Clamp quality-derived iteration counts to a minimum of 1.
  4. Validate the parameter at the boundary where it is parsed.

Example fix

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

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

Strategy: validation

Validate before calling

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

Type guard

public static boolean isPositiveIterations(int steps) {
    return steps > 0;
}

Try / catch

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

Prevention

When it happens

Trigger: Calling getImage(..., maxStep, ...) with maxStep of 0 or negative; passing a quality/precision setting that maps to 0 iterations.

Common situations: A 'quality' slider mapped to a count that can reach 0; CLI/default value unset resolving to 0; logic that subtracts from maxStep until it hits 0.

Related errors


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