{"record":{"id":"d9760a8b311525fd","repo":"TheAlgorithms/Java","slug":"input-lists-must-not-be-null","errorCode":null,"errorMessage":"Input lists must not be null.","messagePattern":"Input lists must not be null\\.","errorType":"exception","errorClass":"NullPointerException","httpStatus":null,"severity":"error","filePath":"src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java","lineNumber":42,"sourceCode":" *\n * @see SinglyLinkedList\n */\npublic class MergeSortedSinglyLinkedList extends SinglyLinkedList {\n\n    /**\n     * Merges two sorted singly linked lists into a single sorted singly linked list.\n     *\n     * <p>This method does not modify the input lists; instead, it creates a new merged linked list\n     * containing all elements from both lists in sorted order.</p>\n     *\n     * @param listA The first sorted singly linked list.\n     * @param listB The second sorted singly linked list.\n     * @return A new singly linked list containing all elements from both lists in sorted order.\n     * @throws NullPointerException if either input list is null.\n     */\n    public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList listB) {\n        if (listA == null || listB == null) {\n            throw new NullPointerException(\"Input lists must not be null.\");\n        }\n\n        SinglyLinkedListNode headA = listA.getHead();\n        SinglyLinkedListNode headB = listB.getHead();\n        int size = listA.size() + listB.size();\n\n        SinglyLinkedListNode head = new SinglyLinkedListNode();\n        SinglyLinkedListNode tail = head;\n        while (headA != null && headB != null) {\n            if (headA.value <= headB.value) {\n                tail.next = headA;\n                headA = headA.next;\n            } else {\n                tail.next = headB;\n                headB = headB.next;\n            }\n            tail = tail.next;\n        }","sourceCodeStart":24,"sourceCodeEnd":60,"githubUrl":"https://github.com/TheAlgorithms/Java/blob/fdfb9a395b310167a66bd29e311e36e0e3e9b964/src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java#L24-L60","documentation":"Thrown by MergeSortedSinglyLinkedList.merge(listA, listB) when either input is null. The method reads headA = listA.getHead() and headB = listB.getHead(), so a null list would NPE immediately. The library rejects null inputs up front with NullPointerException.","triggerScenarios":"Passing null for listA or listB. A list reference from a map/optional that resolved to null. Merging where one side is conditionally populated.","commonSituations":"Optional.orElse(null) used for brevity. Branching that leaves one list null when its case is skipped. Deserialization producing null for an absent field.","solutions":["Pass a non-null empty SinglyLinkedList instead of null when a side has no data.","Null-check both arguments before calling merge.","Provide a single-argument convenience wrapper that substitutes an empty list for null.","Enforce non-null list contracts at the data-source boundary."],"exampleFix":"// before\nSinglyLinkedList merged = MergeSortedSinglyLinkedList.merge(a, b); // b may be null\n\n// after\nSinglyLinkedList merged = MergeSortedSinglyLinkedList.merge(\n    a != null ? a : new SinglyLinkedList(),\n    b != null ? b : new SinglyLinkedList());","handlingStrategy":"validation","validationCode":"if (listA != null && listB != null) {\n    return MergeSortedSinglyLinkedList.merge(listA, listB);\n}","typeGuard":null,"tryCatchPattern":"try {\n    return MergeSortedSinglyLinkedList.merge(listA, listB);\n} catch (NullPointerException e) {\n    // one input was null\n}","preventionTips":["Pass an empty SinglyLinkedList rather than null for absent sides.","Null-check both arguments before merge.","Avoid Optional.orElse(null) feeding the merge."],"tags":["merge","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"}