TheAlgorithms/Java · error · IllegalArgumentException
Array must be non-empty.
Error message
Array must be non-empty.
What it means
Thrown by FindMaxRecursion.max(int[] array, int low, int high) when array.length is 0. This divide-and-conquer method splits the range [low, high] into halves and recurses, terminating when low == high (returning array[low]). An empty array has no valid element to return. Note: the method checks array.length == 0 but does NOT validate that low/high are within bounds — passing an empty array is the only condition that triggers this specific message.
Source
Thrown at src/main/java/com/thealgorithms/maths/FindMaxRecursion.java:17
package com.thealgorithms.maths;
public final class FindMaxRecursion {
private FindMaxRecursion() {
}
/**
* Get max of an array using divide and conquer algorithm
*
* @param array contains elements
* @param low the index of the first element
* @param high the index of the last element
* @return max of {@code array}
*/
public static int max(final int[] array, final int low, final int high) {
if (array.length == 0) {
throw new IllegalArgumentException("Array must be non-empty.");
}
if (low == high) {
return array[low]; // or array[high]
}
int mid = (low + high) >>> 1;
int leftMax = max(array, low, mid); // get max in [low, mid]
int rightMax = max(array, mid + 1, high); // get max in [mid+1, high]
return Math.max(leftMax, rightMax);
}
/**
* Get max of an array using recursion algorithm
*
* @param array contains elements
* @return max value of {@code array}View on GitHub (pinned to fdfb9a395b)
Solutions
- Ensure array.length > 0 before calling max().
- Validate that 0 <= low <= high < array.length in addition to non-emptiness.
- Wrap in a defensive helper that checks both the array and index bounds before delegating.
Example fix
// before
int result = FindMaxRecursion.max(arr, 0, arr.length - 1);
// after
if (arr.length == 0) {
throw new IllegalArgumentException("Cannot compute max of empty array");
}
if (low < 0 || high >= arr.length || low > high) {
throw new IndexOutOfBoundsException("Invalid range [" + low + ", " + high + "]");
}
int result = FindMaxRecursion.max(arr, low, high); Defensive patterns
Strategy: validation
Validate before calling
if (array == null || array.length == 0) {
throw new IllegalArgumentException("Array must be non-empty");
}
if (low < 0 || high >= array.length || low > high) {
throw new IndexOutOfBoundsException("Invalid range [" + low + ", " + high + "]");
}
int result = FindMaxRecursion.max(array, low, high); Type guard
static boolean isValidRange(int[] array, int low, int high) {
return array != null && array.length > 0
&& low >= 0 && high < array.length && low <= high;
} Try / catch
try {
int result = FindMaxRecursion.max(array, low, high);
} catch (IllegalArgumentException e) {
// empty array; handle gracefully
} Prevention
- Validate array non-emptiness AND index bounds before calling.
- Ensure low <= high and both are within [0, length-1].
- Check the array source for emptiness from filtering or partitioning.
When it happens
Trigger: Calling max(new int[0], 0, 0) or max(emptyArray, anyLow, anyHigh). The length check fires regardless of the low/high arguments. Also triggered if low/high are passed as values outside the array bounds without the length being zero (in that case you get a different ArrayIndexOutOfBoundsException, not this message).
Common situations: Passing empty arrays from filtered data. Mismatch between caller-supplied low/high indices and the actual array size. Recursive callers that shrink ranges without checking for emptiness.
Related errors
- array must be non-empty.
- Array must be non-empty.
- Array must be non-empty.
- k must be between 1 and the size of the array
- Input array must have length of at least two
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/89e18c326ed20af5.
Report an issue: GitHub.