{"record":{"id":"ec6bb07c0de765e8","repo":"TheAlgorithms/Java","slug":"cannot-add-null-element-to-the-list","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/CircleLinkedList.java","lineNumber":64,"sourceCode":"    /**\n     * 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 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        if (tail == null) {\n            tail = new Node<>(value, head);\n            head.next = tail;\n        } else {\n            tail.next = new Node<>(value, head);\n            tail = tail.next;\n        }\n        size++;\n    }\n\n    /**\n     * Returns a string representation of the list in the format \"[ element1, element2, ... ]\".\n     * An empty list is represented as \"[]\".\n     *\n     * @return the string representation of the list\n     */\n    public String toString() {","sourceCodeStart":46,"sourceCodeEnd":82,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/CircleLinkedList.java#L46-L82","documentation":"Thrown by CircleLinkedList.append(E) when value is null. The list stores Node<E> wrappers and the circular structure assumes non-null payloads for traversal equality. The library rejects null explicitly with NullPointerException rather than allowing a null to corrupt iteration semantics.","triggerScenarios":"Calling append(null). Appending the result of a lookup that returned null. Appending from a collection that permits nulls.","commonSituations":"Map.get on a missing key. Optional.orElse(null) piped into append. Third-party data with nullable fields inserted without filtering.","solutions":["Filter nulls from the source collection before appending.","Add a null check at the call site before append.","Use Optional and skip append on empty.","Normalize upstream to never produce nulls."],"exampleFix":"// before\nlist.append(map.get(key)); // null when absent\n\n// after\nE v = map.get(key);\nif (v != null) list.append(v);","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 collections before append.","Avoid piping nullable getters directly into the list.","Enforce non-null invariants at the data boundary."],"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"}