TheAlgorithms/Java · error · IllegalArgumentException
Numbers array cannot be empty or null
Error message
Numbers array cannot be empty or null
What it means
AbsoluteMax.getMaxValue(varargs int...) rejects null or empty input because there is no maximum of zero numbers — the loop starts at numbers[0]. A null varargs array (possible when explicitly passing null) or zero arguments would cause a meaningless result or IndexOutOfBoundsException. The check fails fast with a clear contract.
Source
Thrown at src/main/java/com/thealgorithms/maths/AbsoluteMax.java:16
package com.thealgorithms.maths;
public final class AbsoluteMax {
private AbsoluteMax() {
}
/**
* Finds the absolute maximum value among the given numbers.
*
* @param numbers The numbers to compare.
* @return The absolute maximum value.
* @throws IllegalArgumentException If the input array is empty or null.
*/
public static int getMaxValue(int... numbers) {
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("Numbers array cannot be empty or null");
}
int absMax = numbers[0];
for (int i = 1; i < numbers.length; i++) {
if (Math.abs(numbers[i]) > Math.abs(absMax) || (Math.abs(numbers[i]) == Math.abs(absMax) && numbers[i] > absMax)) {
absMax = numbers[i];
}
}
return absMax;
}
}
View on GitHub (pinned to fdfb9a395b)
Solutions
- Check the array is non-null and non-empty before calling; provide a default or error for empty input.
- When converting from a collection, guard: if (list.isEmpty()) return defaultValue;.
- Never call getMaxValue(null); if null is possible, handle it explicitly before the call.
Example fix
// before
int max = AbsoluteMax.getMaxValue(arr); // arr may be empty
// after
if (arr == null || arr.length == 0) {
throw new IllegalArgumentException("need at least one value");
}
int max = AbsoluteMax.getMaxValue(arr); Defensive patterns
Strategy: validation
Validate before calling
if (numbers == null || numbers.length == 0) {
throw new IllegalArgumentException("numbers must be non-null and non-empty");
}
AbsoluteMax.getMaxValue(numbers); Prevention
- Guard empty collections before converting to arrays and passing to varargs methods.
- Never pass null explicitly to a varargs parameter; handle null upstream.
- Provide a sensible default or skip the call when the input set is empty.
When it happens
Trigger: Calling getMaxValue() with no arguments, getMaxValue(null), or passing a null array reference explicitly.
Common situations: Passing a possibly-empty collection converted to an array (e.g. list.stream().mapToInt(...).toArray() on an empty list yields an empty array), calling getMaxValue() with no args by mistake, or forwarding a null array from an upstream computation.
Related errors
- Weights matrix must not be null or empty
- X and Y must be non-null, non-empty, and of the same length.
- Values array cannot be empty or null
- Input cannot be null
- Weights matrix must be square
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/64d9b4230ddc1f1d.
Report an issue: GitHub.