{"record":{"id":"79953148bf4637fa","repo":"TheAlgorithms/Java","slug":"input-array-must-not-be-empty","errorCode":null,"errorMessage":"Input array must not be empty.","messagePattern":"Input array must not be empty\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/FibonacciSearch.java","lineNumber":33,"sourceCode":" * Note: This algorithm requires that the input array be sorted.\n * </p>\n */\n@SuppressWarnings({\"rawtypes\", \"unchecked\"})\npublic class FibonacciSearch implements SearchAlgorithm {\n\n    /**\n     * Finds the index of the specified key in a sorted array using Fibonacci search.\n     *\n     * @param array The sorted array to search.\n     * @param key The element to search for.\n     * @param <T> The type of the elements in the array, which must be comparable.\n     * @throws IllegalArgumentException if the input array is not sorted or empty, or if the key is null.\n     * @return The index of the key if found, otherwise -1.\n     */\n    @Override\n    public <T extends Comparable<T>> int find(T[] array, T key) {\n        if (array.length == 0) {\n            throw new IllegalArgumentException(\"Input array must not be empty.\");\n        }\n        if (!isSorted(array)) {\n            throw new IllegalArgumentException(\"Input array must be sorted.\");\n        }\n        if (key == null) {\n            throw new IllegalArgumentException(\"Key must not be null.\");\n        }\n\n        int fibMinus1 = 1;\n        int fibMinus2 = 0;\n        int fibNumber = fibMinus1 + fibMinus2;\n        int n = array.length;\n\n        while (fibNumber < n) {\n            fibMinus2 = fibMinus1;\n            fibMinus1 = fibNumber;\n            fibNumber = fibMinus2 + fibMinus1;\n        }","sourceCodeStart":15,"sourceCodeEnd":51,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/FibonacciSearch.java#L15-L51","documentation":"Thrown by FibonacciSearch.find(T[], T) when the input array has length 0. Fibonacci search requires at least one element to establish its Fibonacci-based partitioning. The guard runs before the sortedness check, so empty arrays fail first.","triggerScenarios":"Calling find(new Integer[]{}, key); passing a collection-backed array that was emptied before the call; filtering an array down to zero elements then searching.","commonSituations":"Reading search data from a query or stream that returned no rows; defaulting to an empty array when a config list is absent; test fixtures using Collections.emptyList().toArray().","solutions":["Check array.length > 0 (or !isEmpty()) before calling find() and short-circuit with -1 if empty.","Ensure upstream data sources actually populate the array for non-empty result sets.","Return a sentinel (e.g. -1) at your API boundary when the source list is empty rather than delegating to FibonacciSearch."],"exampleFix":"// before\nint idx = new FibonacciSearch().find(arr, key);\n\n// after\nint idx = arr.length == 0 ? -1 : new FibonacciSearch().find(arr, key);","handlingStrategy":"validation","validationCode":"if (array == null || array.length == 0) return -1;","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Short-circuit on empty arrays at the caller rather than inside the search.","Prefer returning -1 (the not-found contract) for empty inputs.","Document that an empty array is a valid 'not found' input at your API."],"tags":["search","input-validation","empty-collection"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}