TheAlgorithms/Java · error · IllegalArgumentException
imageHeight should be greater than zero
Error message
imageHeight should be greater than zero
What it means
Thrown by Mandelbrot.getImage when imageHeight <= 0. The method allocates a BufferedImage of imageWidth x imageHeight and uses imageHeight in the figureHeight computation, so a non-positive height produces a degenerate image and is rejected before allocation.
Source
Thrown at src/main/java/com/thealgorithms/others/Mandelbrot.java:81
* 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;
double figureY = figureCenterY + ((double) imageY / imageHeight - 0.5) * figureHeight;
double distance = getDistance(figureX, figureY, maxStep);
View on GitHub (pinned to fdfb9a395b)
Solutions
- Provide a positive imageHeight (e.g. 600) when calling getImage.
- Default height to a positive value when the source is missing or zero.
- Validate both dimensions at the input boundary before invoking.
- Make sure aspect-ratio computations cannot yield 0.
Example fix
// before BufferedImage img = Mandelbrot.getImage(width, height, cx, cy, fw, maxStep, color); // height may be 0 // after int h = height > 0 ? height : 600; BufferedImage img = Mandelbrot.getImage(width, h, cx, cy, fw, maxStep, color);
Defensive patterns
Strategy: validation
Validate before calling
if (imageHeight <= 0) imageHeight = 600; 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(w, 600, cx, cy, fw, maxStep, color);
} Prevention
- Default height to a positive value at config load.
- Ensure aspect-ratio calculations never produce 0.
- Validate both dimensions at the input boundary.
When it happens
Trigger: Calling getImage(..., imageHeight, ...) with imageHeight of 0 or negative; height read from a config/CLI argument that defaults to 0 when unset.
Common situations: Unset height parameter defaulting to 0; CLI parsing returning 0; portrait/landscape auto-calc producing 0 before a layout pass; negative height from subtraction.
Related errors
- imageWidth should be greater than zero
- imageWidth should be greater than zero
- maxStep should be greater than zero
- Input '{input}' contains not only digits
- Position must be between 0 and {array.length}
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/88c8eaf3dba18550.
Report an issue: GitHub.