{"record":{"id":"27c135406993087b","repo":"TheAlgorithms/Java","slug":"element-cannot-be-null","errorCode":null,"errorMessage":"Element cannot be null","messagePattern":"Element cannot be null","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java","lineNumber":77,"sourceCode":"            int start = head;\n            while (start != -1) {\n                T element = cursorSpace[start].element;\n                System.out.println(element.toString());\n                start = cursorSpace[start].next;\n            }\n        }\n    }\n\n    /**\n     * Finds the logical index of a specified element in the list.\n     *\n     * @param element the element to search for in the list\n     * @return the logical index of the element, or -1 if not found\n     * @throws NullPointerException if element is null\n     */\n    public int indexOf(T element) {\n        if (element == null) {\n            throw new NullPointerException(\"Element cannot be null\");\n        }\n        try {\n            Objects.requireNonNull(element);\n            Node<T> iterator = cursorSpace[head];\n            for (int i = 0; i < count; i++) {\n                if (iterator.element.equals(element)) {\n                    return i;\n                }\n                iterator = cursorSpace[iterator.next];\n            }\n        } catch (Exception e) {\n            return -1;\n        }\n        return -1;\n    }\n\n    /**\n     * Retrieves an element at a specified logical index in the list.","sourceCodeStart":59,"sourceCodeEnd":95,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/CursorLinkedList.java#L59-L95","documentation":"Thrown by CursorLinkedList.indexOf(T) when element is null. The method iterates logical nodes calling iterator.element.equals(element), so a null element would NPE on the equals call. The library rejects null upfront with NullPointerException.","triggerScenarios":"Calling indexOf(null). Searching for an element obtained from a nullable source. Passing the result of Map.get on a missing key.","commonSituations":"User input that was not null-validated. Optional.orElse(null) fed to indexOf. Lookup tables that return null for absent entries.","solutions":["Null-check before calling indexOf.","Wrap the element in Optional and skip the search when empty.","Filter null values out of the data feeding the list and its queries.","Return -1 yourself when the input is null rather than calling indexOf."],"exampleFix":"// before\nint idx = list.indexOf(map.get(key));\n\n// after\nT e = map.get(key);\nint idx = (e == null) ? -1 : list.indexOf(e);","handlingStrategy":"validation","validationCode":"if (element != null) {\n    return list.indexOf(element);\n}\nreturn -1;","typeGuard":null,"tryCatchPattern":"try {\n    return list.indexOf(element);\n} catch (NullPointerException e) {\n    return -1;\n}","preventionTips":["Null-check before indexOf, returning -1 yourself for null input.","Filter nulls from query sources.","Prefer Optional for values that may be absent."],"tags":["linked-list","null-check","data-structure","precondition","java"],"backgroundTag":null,"analyzedSha":"fdfb9a395b310167a66bd29e311e36e0e3e9b964","analyzedAt":"2026-08-13T23:36:13.315Z","schemaVersion":2},"datasetVersion":"2026-08-14T00:17:13.853Z"}