TheAlgorithms/Java · error · IllegalArgumentException

Numbers array cannot be empty or null

Error message

Numbers array cannot be empty or null

What it means

Thrown by AbsoluteMin.getMinValue when the varargs 'numbers' parameter is null or has length 0. The method needs at least one value to seed 'absMin' (numbers[0]); with an empty array that seed read would throw ArrayIndexOutOfBounds, so the guard converts that into a clear precondition failure. It is the only entry point to the class, so any caller passing no values hits it.

Source

Thrown at src/main/java/com/thealgorithms/maths/AbsoluteMin.java:15

package com.thealgorithms.maths;

public final class AbsoluteMin {
    private AbsoluteMin() {
    }

    /**
     * Compares the numbers given as arguments to get the absolute min value.
     *
     * @param numbers The numbers to compare
     * @return The absolute min value
     */
    public static int getMinValue(int... numbers) {
        if (numbers == null || numbers.length == 0) {
            throw new IllegalArgumentException("Numbers array cannot be empty or null");
        }

        long absMin = numbers[0];
        for (int i = 1; i < numbers.length; i++) {
            long current = numbers[i];
            if (Math.abs(current) < Math.abs(absMin) || (Math.abs(current) == Math.abs(absMin) && current < absMin)) {
                absMin = current;
            }
        }
        return (int) absMin;
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass at least one element: getMinValue(7) or getMinValue(arr) with a non-empty arr.
  2. Guard before calling: if (arr != null && arr.length > 0) { ... getMinValue(arr) ... } else { handle empty }.
  3. Pick a default/early-return when the source collection is empty rather than forwarding it.
  4. If the array may be null from an upstream API, null-check and coalesce to a sentinel or skip the call.

Example fix

// before
int m = AbsoluteMin.getMinValue(filtered.toArray());
// after
int[] arr = filtered.stream().mapToInt(Integer::intValue).toArray();
if (arr.length == 0) { throw new IllegalStateException("no samples"); }
int m = AbsoluteMin.getMinValue(arr);
Defensive patterns

Strategy: validation

Validate before calling

if (numbers == null || numbers.length == 0) {
    throw new IllegalStateException("cannot compute absolute min of no values");
}
int min = AbsoluteMin.getMinValue(numbers);

Type guard

static boolean hasValues(int... numbers) {
    return numbers != null && numbers.length > 0;
}

Try / catch

try {
    int min = AbsoluteMin.getMinValue(arr);
} catch (IllegalArgumentException e) {
    // arr was null/empty; provide a default or report to caller
    min = defaultValue;
}

Prevention

When it happens

Trigger: Calling getMinValue() with zero arguments; passing (int[]) null; passing an int[] that was filtered down to length 0 (e.g. stream().filter().toArray() that matched nothing).

Common situations: Computing a minimum over a dynamic/filtered collection where the filter can yield nothing; unit tests that call the no-arg form; deserialized arrays that came back null; refactoring a fixed list into a stream pipeline that lost the empty case.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/e4b640e68e7e63bd. Report an issue: GitHub.