{"record":{"id":"0fa9ba2dd0867203","repo":"TheAlgorithms/Java","slug":"the-list-of-elements-must-not-be-empty","errorCode":null,"errorMessage":"The list of elements must not be empty.","messagePattern":"The list of elements must not be empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/QuickSelect.java","lineNumber":36,"sourceCode":"     * be at index n if the list was sorted.\n     * <p>\n     * Calling this function might change the order of elements in {@code list}.\n     *\n     * @param list the list of elements\n     * @param n    the index\n     * @param <T>  the type of list elements\n     * @return the n-th largest element in the list\n     * @throws IndexOutOfBoundsException if n is less than 0 or greater or equal to\n     *                                   the number of elements in the list\n     * @throws IllegalArgumentException  if the list is empty\n     * @throws NullPointerException      if {@code list} is null\n     */\n    public static <T extends Comparable<T>> T select(List<T> list, int n) {\n        Objects.requireNonNull(list, \"The list of elements must not be null.\");\n\n        if (list.isEmpty()) {\n            String msg = \"The list of elements must not be empty.\";\n            throw new IllegalArgumentException(msg);\n        }\n\n        if (n < 0) {\n            String msg = \"The index must not be negative.\";\n            throw new IndexOutOfBoundsException(msg);\n        }\n\n        if (n >= list.size()) {\n            String msg = \"The index must be less than the number of elements.\";\n            throw new IndexOutOfBoundsException(msg);\n        }\n\n        int index = selectIndex(list, n);\n        return list.get(index);\n    }\n\n    private static <T extends Comparable<T>> int selectIndex(List<T> list, int n) {\n        return selectIndex(list, 0, list.size() - 1, n);","sourceCodeStart":18,"sourceCodeEnd":54,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/QuickSelect.java#L18-L54","documentation":"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.","triggerScenarios":"Calling select(Collections.emptyList(), n); passing a filtered/sized list that ended up empty; calling with a newly constructed ArrayList that was never populated.","commonSituations":"Querying a repository that returned no results; processing a batch where all items were filtered out; defaulting to an empty list instead of null.","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."],"exampleFix":"// before\nT nth = QuickSelect.select(items, n);\n\n// after\nif (items.isEmpty()) return Optional.<T>empty();\nT nth = QuickSelect.select(items, n);","handlingStrategy":"validation","validationCode":"if (list == null || list.isEmpty()) return Optional.<T>empty();","typeGuard":"public static <T> boolean isNonEmpty(List<T> list) { return list != null && !list.isEmpty(); }","tryCatchPattern":null,"preventionTips":["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."],"tags":["selection","empty-collection","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}