TheAlgorithms/Java · error · IllegalArgumentException

Values array cannot be empty or null

Error message

Values array cannot be empty or null

What it means

Thrown by median(int[] values) when the input array is null or empty. The median of zero elements is undefined. The method also sorts the array in place, so a null array would cause a NullPointerException at Arrays.sort without this guard. Note: this method mutates the input array.

Source

Thrown at src/main/java/com/thealgorithms/maths/Median.java:38

 */
public final class Median {
    private Median() {
    }

    /**
     * Calculates the median of an array of integers.
     * The array is sorted internally, so the original order is not preserved.
     * For arrays with an odd number of elements, returns the middle element.
     * For arrays with an even number of elements, returns the average of the two
     * middle elements.
     *
     * @param values the array of integers to find the median of (can be unsorted)
     * @return the median value as a double
     * @throws IllegalArgumentException if the input array is empty or null
     */
    public static double median(int[] values) {
        if (values == null || values.length == 0) {
            throw new IllegalArgumentException("Values array cannot be empty or null");
        }

        Arrays.sort(values);
        int length = values.length;
        if (length % 2 == 0) {
            return (values[length / 2] + values[length / 2 - 1]) / 2.0;
        } else {
            return values[length / 2];
        }
    }
}

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Check that values != null && values.length > 0 before calling median
  2. Handle the empty-array case at the data layer (return Optional.empty or a default)
  3. If the caller needs to preserve the original order, pass a copy since median() sorts in place

Example fix

// before
double m = Median.median(data);

// after
if (data == null || data.length == 0) {
    throw new IllegalArgumentException("Cannot compute median of empty array");
}
double m = Median.median(data);
Defensive patterns

Strategy: validation

Validate before calling

if (values == null || values.length == 0) {
    throw new IllegalArgumentException("Cannot compute median of empty or null array");
}
double m = Median.median(values);

Type guard

static boolean hasElements(int[] arr) {
    return arr != null && arr.length > 0;
}

Try / catch

try {
    double m = Median.median(values);
} catch (IllegalArgumentException e) {
    // handle empty/null input
    return Optional.empty();
}

Prevention

When it happens

Trigger: Calling Median.median(null) or Median.median(new int[0]). Also hit when an array is conditionally populated and ends up empty.

Common situations: Processing query results or stream-collected arrays that may be empty. Passing a filtered array where all elements were removed. Forgetting that median() sorts in place and may receive a shared array reference.

Related errors


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