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
- Check that values != null && values.length > 0 before calling median
- Handle the empty-array case at the data layer (return Optional.empty or a default)
- 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
- Median.median sorts the array in place — pass a copy if the caller needs the original order
- Null arrays cause a different error path than empty arrays, so check both
- Guard data-loading code to reject empty result sets before they reach the median call
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
- Empty list given for Mean computation.
- Numbers array cannot be empty or null
- Number must be non-negative. Given:
- Number must be positive
- Input must be non-negative. Received:
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/ceabfe2441c4bbd3.
Report an issue: GitHub.