{"record":{"id":"e3792360eb847a41","repo":"TheAlgorithms/Java","slug":"key-must-not-be-null-e37923","errorCode":null,"errorMessage":"Key must not be null.","messagePattern":"Key must not be null\\.","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/FibonacciSearch.java","lineNumber":39,"sourceCode":"    /**\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        }\n\n        int offset = -1;\n\n        while (fibNumber > 1) {\n            int i = Math.min(offset + fibMinus2, n - 1);\n","sourceCodeStart":21,"sourceCodeEnd":57,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/FibonacciSearch.java#L21-L57","documentation":"Thrown by FibonacciSearch.find(T[], T) when key == null. The search compares elements using Comparable.compareTo, which cannot meaningfully compare against null and would throw NullPointerException internally. The guard converts this into a clear precondition failure.","triggerScenarios":"Calling find(arr, null); passing a key obtained from a Map.get() that returned null; using a nullable reference without null-checking.","commonSituations":"Lookups keyed by optional/user input where the field was never set; deserialized DTOs with null fields; chaining methods that may return null.","solutions":["Null-check the key before calling find() and decide on a -1 or empty result per your contract.","Use Optional or default sentinel values instead of null for the search key.","Annotate the key parameter with @NonNull and enable static null analysis."],"exampleFix":"// before\nint idx = new FibonacciSearch().find(arr, key);\n\n// after\nif (key == null) return -1;\nint idx = new FibonacciSearch().find(arr, key);","handlingStrategy":"validation","validationCode":"if (key == null) return -1;","typeGuard":"public static boolean isSearchableKey(Object key) { return key != null; }","tryCatchPattern":null,"preventionTips":["Disallow null search keys at your API boundary.","Use Optional or sentinel values instead of null for keys.","Enable static null analysis (@NonNull on the key parameter)."],"tags":["search","nullable","input-validation","null-check"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}