{"record":{"id":"30c83302f335091d","repo":"TheAlgorithms/Java","slug":"array-is-empty-30c833","errorCode":null,"errorMessage":"Array is empty","messagePattern":"Array is empty","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/SaddlebackSearch.java","lineNumber":34,"sourceCode":" */\npublic final class SaddlebackSearch {\n    private SaddlebackSearch() {\n    }\n\n    /**\n     * This method performs Saddleback Search\n     *\n     * @param arr The **Sorted** array in which we will search the element.\n     * @param row the current row.\n     * @param col the current column.\n     * @param key the element that we want to search for.\n     * @throws IllegalArgumentException if the array is empty.\n     * @return The index(row and column) of the element if found. Else returns\n     * -1 -1.\n     */\n    static int[] find(int[][] arr, int row, int col, int key) {\n        if (arr.length == 0) {\n            throw new IllegalArgumentException(\"Array is empty\");\n        }\n\n        // array to store the answer row and column\n        int[] ans = {-1, -1};\n        if (row < 0 || col >= arr[row].length) {\n            return ans;\n        }\n        if (arr[row][col] == key) {\n            ans[0] = row;\n            ans[1] = col;\n            return ans;\n        } // if the current element is greater than the given element then we move up\n        else if (arr[row][col] > key) {\n            return find(arr, row - 1, col, key);\n        }\n        // else we move right\n        return find(arr, row, col + 1, key);\n    }","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/SaddlebackSearch.java#L16-L52","documentation":"Thrown by SaddlebackSearch.find(int[][], int, int, int) when arr.length == 0. Saddleback search operates on a 2D sorted matrix and needs at least one row to begin. An empty outer array means there is no matrix to search, so the method rejects it.","triggerScenarios":"Calling find(new int[0][], ...); passing a matrix built from an empty row set; deserializing a matrix that came back with zero rows.","commonSituations":"Matrix loaded from a CSV/file that was empty; downstream computation produced no rows; defaulting to an empty matrix instead of null.","solutions":["Check arr.length > 0 before calling find() and return a not-found result {-1, -1} directly.","Validate the matrix at construction/loading time and reject empty inputs upstream.","Use a wrapper that normalizes null/empty matrices to a documented not-found result."],"exampleFix":"// before\nint[] pos = SaddlebackSearch.find(matrix, 0, cols - 1, key);\n\n// after\nint[] pos = matrix.length == 0 ? new int[]{-1, -1} : SaddlebackSearch.find(matrix, 0, cols - 1, key);","handlingStrategy":"validation","validationCode":"if (arr == null || arr.length == 0) return new int[]{-1, -1};","typeGuard":"public static boolean isNonEmptyMatrix(int[][] m) { return m != null && m.length > 0; }","tryCatchPattern":null,"preventionTips":["Treat an empty matrix as an immediate not-found result.","Validate matrix dimensions at load/construction time.","Document that zero-row matrices are rejected by SaddlebackSearch."],"tags":["search","matrix","empty-collection","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}