{"record":{"id":"5256061e3604a9dc","repo":"TheAlgorithms/Java","slug":"array-cannot-be-null-525606","errorCode":null,"errorMessage":"Array cannot be null","messagePattern":"Array cannot be null","errorType":"exception","errorClass":"IllegalArgumentException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/searches/SentinelLinearSearch.java","lineNumber":49,"sourceCode":" *\n * @author TheAlgorithms Contributors\n * @see LinearSearch\n * @see SearchAlgorithm\n */\npublic class SentinelLinearSearch implements SearchAlgorithm {\n    /**\n     * Performs sentinel linear search on the given array.\n     *\n     * @param array the array to search in\n     * @param key the element to search for\n     * @param <T> the type of elements in the array, must be Comparable\n     * @return the index of the first occurrence of the key, or -1 if not found\n     * @throws IllegalArgumentException if the array is null\n     */\n    @Override\n    public <T extends Comparable<T>> int find(T[] array, T key) {\n        if (array == null) {\n            throw new IllegalArgumentException(\"Array cannot be null\");\n        }\n\n        if (array.length == 0) {\n            return -1;\n        }\n\n        if (key == null) {\n            return findNull(array);\n        }\n\n        // Store the last element\n        T lastElement = array[array.length - 1];\n\n        // Place the sentinel (search key) at the end\n        array[array.length - 1] = key;\n\n        int i = 0;\n        // Search without bound checking since sentinel guarantees we'll find the key","sourceCodeStart":31,"sourceCodeEnd":67,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/searches/SentinelLinearSearch.java#L31-L67","documentation":"Thrown by SentinelLinearSearch.find(T[], T) when array == null. The sentinel technique writes a sentinel into the last array slot, which dereferences the array reference, so a null array would cause a NullPointerException. The guard converts it into a clear IllegalArgumentException.","triggerScenarios":"Calling find(null, key); passing a field that was never initialized; receiving null from a loader or Map.get() and forwarding it.","commonSituations":"Optional fields left null; data sources that return null instead of an empty array; legacy code using null as a sentinel for 'no data'.","solutions":["Null-check the array before calling find() and return -1 or an empty result.","Initialize fields to empty arrays rather than null.","Use Optional or @NonNull annotations to make nullability explicit at the type level."],"exampleFix":"// before\nint idx = new SentinelLinearSearch().find(arr, key);\n\n// after\nif (arr == null) return -1;\nint idx = new SentinelLinearSearch().find(arr, key);","handlingStrategy":"validation","validationCode":"if (array == null) return -1;","typeGuard":"public static <T> boolean isSearchable(T[] array) { return array != null; }","tryCatchPattern":null,"preventionTips":["Initialize array fields to empty arrays, not null.","Null-check at the API boundary before delegating to SentinelLinearSearch.","Use @NonNull annotations and static analysis to catch null propagation."],"tags":["search","nullable","null-check","input-validation"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}