{"record":{"id":"e54b393e593cb42d","repo":"TheAlgorithms/Java","slug":"cannot-add-null-element-to-the-list-e54b39","errorCode":null,"errorMessage":"Cannot add null element to the list","messagePattern":"Cannot add null element to the list","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/CircularDoublyLinkedList.java","lineNumber":64,"sourceCode":"     * Returns the current size of the list.\n     *\n     * @return the number of elements in the list\n     */\n    public int getSize() {\n        return size;\n    }\n\n    /**\n     * Appends a new element to the end of the list. Throws a NullPointerException\n     * if\n     * a null value is provided.\n     *\n     * @param value the value to append to the list\n     * @throws NullPointerException if the value is null\n     */\n    public void append(E value) {\n        if (value == null) {\n            throw new NullPointerException(\"Cannot add null element to the list\");\n        }\n        Node<E> newNode = new Node<>(value, head, head.prev);\n        head.prev.next = newNode;\n        head.prev = newNode;\n        size++;\n    }\n\n    /**\n     * Returns a string representation of the list in the format \"[ element1,\n     * element2, ... ]\".\n     * An empty list is represented as \"[]\".\n     *\n     * @return the string representation of the list\n     */\n    public String toString() {\n        if (size == 0) {\n            return \"[]\";\n        }","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/CircularDoublyLinkedList.java#L46-L82","documentation":"Thrown by CircularDoublyLinkedList.append(E) when value is null. The list links nodes bidirectionally through head.prev/head.next and assumes non-null payloads. The library rejects null with NullPointerException to keep node-walking and equality semantics sound.","triggerScenarios":"Calling append(null). Inserting values from a stream or collection that may contain null. Appending the result of a nullable getter.","commonSituations":"Deserialized objects with optional fields. Map lookups returning null for absent keys. APIs that return null on failure piped directly into append.","solutions":["Filter nulls before appending: `coll.stream().filter(Objects::nonNull).forEach(list::append)`.","Null-check at the call site.","Replace nullable sources with Optional and skip on empty.","Document and enforce non-null contracts upstream."],"exampleFix":"// before\nlist.append(value); // value may be null\n\n// after\nif (value != null) {\n    list.append(value);\n}","handlingStrategy":"validation","validationCode":"if (value != null) {\n    list.append(value);\n}","typeGuard":null,"tryCatchPattern":"try {\n    list.append(value);\n} catch (NullPointerException e) {\n    // value was null — filter upstream\n}","preventionTips":["Filter nulls from source data before append.","Substitute Optional and skip on empty.","Document the non-null contract for the list."],"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"}