{"record":{"id":"6435d0b031755626","repo":"TheAlgorithms/Java","slug":"width-and-height-must-be-0","errorCode":null,"errorMessage":"width and height must be > 0","messagePattern":"width and height must be > 0","errorType":"validation","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/others/PerlinNoise.java","lineNumber":55,"sourceCode":"\npublic final class PerlinNoise {\n    private PerlinNoise() {\n    }\n\n    /**\n     * Generate a 2D array of blended noise values normalized to [0, 1].\n     *\n     * @param width       width of the noise array (columns)\n     * @param height      height of the noise array (rows)\n     * @param octaveCount number of octaves (layers) to blend; must be >= 1\n     * @param persistence per-octave amplitude multiplier in (0, 1]\n     * @param seed        seed for the random base grid\n     * @return a {@code width x height} array containing blended noise values in [0,\n     *         1]\n     */\n    static float[][] generatePerlinNoise(int width, int height, int octaveCount, float persistence, long seed) {\n        if (width <= 0 || height <= 0) {\n            throw new IllegalArgumentException(\"width and height must be > 0\");\n        }\n\n        if (octaveCount < 1) {\n            throw new IllegalArgumentException(\"octaveCount must be >= 1\");\n        }\n        if (!(persistence > 0f && persistence <= 1f)) { // using > to exclude 0 and NaN\n            throw new IllegalArgumentException(\"persistence must be in (0, 1]\");\n        }\n        final float[][] base = createBaseGrid(width, height, seed);\n        final float[][][] layers = createLayers(base, width, height, octaveCount);\n        return blendAndNormalize(layers, width, height, persistence);\n    }\n\n    /** Create the base random lattice values in [0,1). */\n    static float[][] createBaseGrid(int width, int height, long seed) {\n        final float[][] base = new float[width][height];\n        Random random = new Random(seed);\n        for (int x = 0; x < width; x++) {","sourceCodeStart":37,"sourceCodeEnd":73,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/others/PerlinNoise.java#L37-L73","documentation":"Thrown by PerlinNoise.generatePerlinNoise when width <= 0 or height <= 0. The method allocates a width x height noise grid and indexes it per coordinate, so a non-positive dimension would create an empty/invalid array and cause downstream indexing failures; it is rejected before allocation.","triggerScenarios":"Calling generatePerlinNoise(width, height, ...) with width <= 0 or height <= 0; dimensions sourced from config/CLI that default to 0 when unset.","commonSituations":"Unset dimension parameters defaulting to 0; CLI parsing returning 0; texture/tile size computed to 0 before a layout pass; negative dimension from an arithmetic error.","solutions":["Provide positive width and height (e.g. 64x64) when calling.","Default both dimensions to a positive value when the source is missing or zero.","Validate dimensions at the input boundary and fail with context there.","Ensure size computations (e.g. baseSize * scale) cannot collapse to 0."],"exampleFix":"// before\nfloat[][] noise = PerlinNoise.generatePerlinNoise(w, h, octaves, persistence, seed); // w or h may be 0\n\n// after\nint width = w > 0 ? w : 64;\nint height = h > 0 ? h : 64;\nfloat[][] noise = PerlinNoise.generatePerlinNoise(width, height, octaves, persistence, seed);","handlingStrategy":"validation","validationCode":"int w = width > 0 ? width : 64;\nint h = height > 0 ? height : 64;\nfloat[][] noise = PerlinNoise.generatePerlinNoise(w, h, octaveCount, persistence, seed);","typeGuard":"public static boolean arePositiveDimensions(int w, int h) {\n    return w > 0 && h > 0;\n}","tryCatchPattern":"try {\n    noise = PerlinNoise.generatePerlinNoise(w, h, oct, p, seed);\n} catch (IllegalArgumentException e) {\n    noise = PerlinNoise.generatePerlinNoise(64, 64, oct, p, seed);\n}","preventionTips":["Default both dimensions to a positive value at config load.","Ensure size computations (baseSize * scale) cannot collapse to 0.","Validate dimensions at the input boundary."],"tags":["validation","perlin-noise","procedural-generation","input-validation","array"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}