TheAlgorithms/Java · error · IllegalArgumentException
The list of elements must not be empty.
Error message
The list of elements must not be empty.
What it means
Thrown by QuickSelect.select(List, int) when the list is empty. QuickSelect partitions the list to find the n-th largest element, which is undefined for an empty list. The guard fires after the null check but before index validation.
Source
Thrown at src/main/java/com/thealgorithms/searches/QuickSelect.java:36
* be at index n if the list was sorted.
* <p>
* Calling this function might change the order of elements in {@code list}.
*
* @param list the list of elements
* @param n the index
* @param <T> the type of list elements
* @return the n-th largest element in the list
* @throws IndexOutOfBoundsException if n is less than 0 or greater or equal to
* the number of elements in the list
* @throws IllegalArgumentException if the list is empty
* @throws NullPointerException if {@code list} is null
*/
public static <T extends Comparable<T>> T select(List<T> list, int n) {
Objects.requireNonNull(list, "The list of elements must not be null.");
if (list.isEmpty()) {
String msg = "The list of elements must not be empty.";
throw new IllegalArgumentException(msg);
}
if (n < 0) {
String msg = "The index must not be negative.";
throw new IndexOutOfBoundsException(msg);
}
if (n >= list.size()) {
String msg = "The index must be less than the number of elements.";
throw new IndexOutOfBoundsException(msg);
}
int index = selectIndex(list, n);
return list.get(index);
}
private static <T extends Comparable<T>> int selectIndex(List<T> list, int n) {
return selectIndex(list, 0, list.size() - 1, n);View on GitHub (pinned to fdfb9a395b)
Solutions
- Check list.isEmpty() before calling select() and return a default/empty Optional instead.
- Ensure upstream producers guarantee a non-empty list for this code path.
- Wrap the call site in a precondition that reports which source produced the empty list.
Example fix
// before T nth = QuickSelect.select(items, n); // after if (items.isEmpty()) return Optional.<T>empty(); T nth = QuickSelect.select(items, n);
Defensive patterns
Strategy: validation
Validate before calling
if (list == null || list.isEmpty()) return Optional.<T>empty();
Type guard
public static <T> boolean isNonEmpty(List<T> list) { return list != null && !list.isEmpty(); } Prevention
- Return Optional or a default instead of calling select on an empty list.
- Guarantee non-empty inputs from upstream producers for this code path.
- Add a precondition check that names the empty source for easier debugging.
When it happens
Trigger: Calling select(Collections.emptyList(), n); passing a filtered/sized list that ended up empty; calling with a newly constructed ArrayList that was never populated.
Common situations: Querying a repository that returned no results; processing a batch where all items were filtered out; defaulting to an empty list instead of null.
Related errors
- Input array must not be empty.
- The index must not be negative.
- The index must be less than the number of elements.
- Array is empty
- Theta (angle) must be a finite number.
AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13).
Data as JSON: /api/errors/0fa9ba2dd0867203.
Report an issue: GitHub.