{"record":{"id":"af6a6a173d4f4726","repo":"TheAlgorithms/Java","slug":"imagewidth-should-be-greater-than-zero","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/KochSnowflake.java","lineNumber":102,"sourceCode":"    public static ArrayList<Vector2> iterate(ArrayList<Vector2> initialVectors, int steps) {\n        ArrayList<Vector2> vectors = initialVectors;\n        for (int i = 0; i < steps; i++) {\n            vectors = iterationStep(vectors);\n        }\n\n        return vectors;\n    }\n\n    /**\n     * Method to render the Koch snowflake to a image.\n     *\n     * @param imageWidth The width of the rendered image.\n     * @param steps The number of iterations.\n     * @return The image of the rendered Koch snowflake.\n     */\n    public static BufferedImage getKochSnowflake(int imageWidth, int steps) {\n        if (imageWidth <= 0) {\n            throw new IllegalArgumentException(\"imageWidth should be greater than zero\");\n        }\n\n        double offsetX = imageWidth / 10.;\n        double offsetY = imageWidth / 3.7;\n        Vector2 vector1 = new Vector2(offsetX, offsetY);\n        Vector2 vector2 = new Vector2(imageWidth / 2.0, Math.sin(Math.PI / 3.0) * imageWidth * 0.8 + offsetY);\n        Vector2 vector3 = new Vector2(imageWidth - offsetX, offsetY);\n        ArrayList<Vector2> initialVectors = new ArrayList<Vector2>();\n        initialVectors.add(vector1);\n        initialVectors.add(vector2);\n        initialVectors.add(vector3);\n        initialVectors.add(vector1);\n        ArrayList<Vector2> vectors = iterate(initialVectors, steps);\n        return getImage(vectors, imageWidth, imageWidth);\n    }\n\n    /**\n     * Loops through each pair of adjacent vectors. Each line between two","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/KochSnowflake.java#L84-L120","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Supply a positive imageWidth (e.g. 256 or 512) before calling.","Default the width to a sensible positive value when the source config is missing/zero.","Validate the parameter at the boundary (config load / CLI parse) and fail there with context.","Ensure the UI control never offers 0 as a selectable size."],"exampleFix":"// before\nBufferedImage img = KochSnowflake.getKochSnowflake(configuredWidth, steps); // configuredWidth may be 0\n\n// after\nint width = configuredWidth > 0 ? configuredWidth : 512;\nBufferedImage img = KochSnowflake.getKochSnowflake(width, steps);","handlingStrategy":"validation","validationCode":"if (imageWidth <= 0) throw new IllegalArgumentException(\"imageWidth must be > 0; got \" + imageWidth);\nBufferedImage img = KochSnowflake.getKochSnowflake(imageWidth, steps);","typeGuard":"public static boolean isPositiveDimension(int dim) {\n    return dim > 0;\n}","tryCatchPattern":"try {\n    img = KochSnowflake.getKochSnowflake(width, steps);\n} catch (IllegalArgumentException e) {\n    img = KochSnowflake.getKochSnowflake(512, steps); // sane default\n}","preventionTips":["Default image dimensions to a positive value at config load.","Constrain UI size controls to a minimum of 1.","Validate dimensions where they enter the system (CLI/config), not deep inside."],"tags":["validation","graphics","image","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T05:17:29.042Z"}