TheAlgorithms/Java · error · IllegalArgumentException
width and height must be > 0
Error message
width and height must be > 0
What it means
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.
Source
Thrown at src/main/java/com/thealgorithms/others/PerlinNoise.java:55
public final class PerlinNoise {
private PerlinNoise() {
}
/**
* Generate a 2D array of blended noise values normalized to [0, 1].
*
* @param width width of the noise array (columns)
* @param height height of the noise array (rows)
* @param octaveCount number of octaves (layers) to blend; must be >= 1
* @param persistence per-octave amplitude multiplier in (0, 1]
* @param seed seed for the random base grid
* @return a {@code width x height} array containing blended noise values in [0,
* 1]
*/
static float[][] generatePerlinNoise(int width, int height, int octaveCount, float persistence, long seed) {
if (width <= 0 || height <= 0) {
throw new IllegalArgumentException("width and height must be > 0");
}
if (octaveCount < 1) {
throw new IllegalArgumentException("octaveCount must be >= 1");
}
if (!(persistence > 0f && persistence <= 1f)) { // using > to exclude 0 and NaN
throw new IllegalArgumentException("persistence must be in (0, 1]");
}
final float[][] base = createBaseGrid(width, height, seed);
final float[][][] layers = createLayers(base, width, height, octaveCount);
return blendAndNormalize(layers, width, height, persistence);
}
/** Create the base random lattice values in [0,1). */
static float[][] createBaseGrid(int width, int height, long seed) {
final float[][] base = new float[width][height];
Random random = new Random(seed);
for (int x = 0; x < width; x++) {View on GitHub (pinned to fdfb9a395b)
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.
Example fix
// before float[][] noise = PerlinNoise.generatePerlinNoise(w, h, octaves, persistence, seed); // w or h may be 0 // after int width = w > 0 ? w : 64; int height = h > 0 ? h : 64; float[][] noise = PerlinNoise.generatePerlinNoise(width, height, octaves, persistence, seed);
Defensive patterns
Strategy: validation
Validate before calling
int w = width > 0 ? width : 64; int h = height > 0 ? height : 64; float[][] noise = PerlinNoise.generatePerlinNoise(w, h, octaveCount, persistence, seed);
Type guard
public static boolean arePositiveDimensions(int w, int h) {
return w > 0 && h > 0;
} Try / catch
try {
noise = PerlinNoise.generatePerlinNoise(w, h, oct, p, seed);
} catch (IllegalArgumentException e) {
noise = PerlinNoise.generatePerlinNoise(64, 64, oct, p, seed);
} Prevention
- 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.
When it happens
Trigger: Calling generatePerlinNoise(width, height, ...) with width <= 0 or height <= 0; dimensions sourced from config/CLI that default to 0 when unset.
Common situations: 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.
Related errors
- octaveCount must be >= 1
- Position must be between 0 and {array.length}
- Array is empty
- Position must be between 0 and + (array.length - 1)
- Input array should not contain negative number(s).
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/6435d0b031755626.
Report an issue: GitHub.