{"record":{"id":"510e3727825e460c","repo":"TheAlgorithms/Java","slug":"imagewidth-should-be-greater-than-zero-510e37","errorCode":null,"errorMessage":"imageWidth should be greater than zero","messagePattern":"imageWidth should be greater than zero","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/Mandelbrot.java","lineNumber":77,"sourceCode":"     * coordinates are used: image-coordinates that refer to the pixels and\n     * figure-coordinates that refer to the complex numbers inside and outside\n     * the Mandelbrot set. The figure-coordinates in the arguments of this\n     * method determine which section of the Mandelbrot set is viewed. The main\n     * area of the Mandelbrot set is roughly between \"-1.5 < x < 0.5\" and \"-1 <\n     * y < 1\" in the figure-coordinates.\n     *\n     * @param imageWidth The width of the rendered image.\n     * @param imageHeight The height of the rendered image.\n     * @param figureCenterX The x-coordinate of the center of the figure.\n     * @param figureCenterY The y-coordinate of the center of the figure.\n     * @param figureWidth The width of the figure.\n     * @param maxStep Maximum number of steps to check for divergent behavior.\n     * @param useDistanceColorCoding Render in color or black and white.\n     * @return The image of the rendered Mandelbrot set.\n     */\n    public static BufferedImage getImage(int imageWidth, int imageHeight, double figureCenterX, double figureCenterY, double figureWidth, int maxStep, boolean useDistanceColorCoding) {\n        if (imageWidth <= 0) {\n            throw new IllegalArgumentException(\"imageWidth should be greater than zero\");\n        }\n\n        if (imageHeight <= 0) {\n            throw new IllegalArgumentException(\"imageHeight should be greater than zero\");\n        }\n\n        if (maxStep <= 0) {\n            throw new IllegalArgumentException(\"maxStep should be greater than zero\");\n        }\n\n        BufferedImage image = new BufferedImage(imageWidth, imageHeight, BufferedImage.TYPE_INT_RGB);\n        double figureHeight = figureWidth / imageWidth * imageHeight;\n\n        // loop through the image-coordinates\n        for (int imageX = 0; imageX < imageWidth; imageX++) {\n            for (int imageY = 0; imageY < imageHeight; imageY++) {\n                // determine the figure-coordinates based on the image-coordinates\n                double figureX = figureCenterX + ((double) imageX / imageWidth - 0.5) * figureWidth;","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/Mandelbrot.java#L59-L95","documentation":"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.","triggerScenarios":"Calling getImage(imageWidth, ...) with imageWidth of 0 or negative; width sourced from a config/CLI argument that defaults to 0 when unset.","commonSituations":"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.","solutions":["Provide a positive imageWidth (e.g. 800) when calling getImage.","Default to a sensible positive width when the source value is missing or zero.","Validate dimensions at the input boundary and fail with a clear message there.","Ensure any interactive control constrains width to a minimum of 1."],"exampleFix":"// before\nBufferedImage img = Mandelbrot.getImage(width, height, cx, cy, fw, maxStep, color); // width may be 0\n\n// after\nint w = width > 0 ? width : 800;\nBufferedImage img = Mandelbrot.getImage(w, height, cx, cy, fw, maxStep, color);","handlingStrategy":"validation","validationCode":"if (imageWidth <= 0) imageWidth = 800;\nBufferedImage img = Mandelbrot.getImage(imageWidth, imageHeight, cx, cy, fw, maxStep, color);","typeGuard":"public static boolean isPositiveDimension(int dim) {\n    return dim > 0;\n}","tryCatchPattern":"try {\n    img = Mandelbrot.getImage(w, h, cx, cy, fw, maxStep, color);\n} catch (IllegalArgumentException e) {\n    img = Mandelbrot.getImage(800, h, cx, cy, fw, maxStep, color);\n}","preventionTips":["Default width to a positive value at config load.","Validate all three getImage parameters together before calling.","Constrain UI size controls to a minimum of 1."],"tags":["validation","graphics","image","fractal","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}