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

  1. Check the array is non-null and non-empty before calling; provide a default or error for empty input.
  2. When converting from a collection, guard: if (list.isEmpty()) return defaultValue;.
  3. 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

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


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