TheAlgorithms/Java · error · NullPointerException

Input lists must not be null.

Error message

Input lists must not be null.

What it means

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.

Source

Thrown at src/main/java/com/thealgorithms/datastructures/lists/MergeSortedSinglyLinkedList.java:42

 *
 * @see SinglyLinkedList
 */
public class MergeSortedSinglyLinkedList extends SinglyLinkedList {

    /**
     * Merges two sorted singly linked lists into a single sorted singly linked list.
     *
     * <p>This method does not modify the input lists; instead, it creates a new merged linked list
     * containing all elements from both lists in sorted order.</p>
     *
     * @param listA The first sorted singly linked list.
     * @param listB The second sorted singly linked list.
     * @return A new singly linked list containing all elements from both lists in sorted order.
     * @throws NullPointerException if either input list is null.
     */
    public static SinglyLinkedList merge(SinglyLinkedList listA, SinglyLinkedList listB) {
        if (listA == null || listB == null) {
            throw new NullPointerException("Input lists must not be null.");
        }

        SinglyLinkedListNode headA = listA.getHead();
        SinglyLinkedListNode headB = listB.getHead();
        int size = listA.size() + listB.size();

        SinglyLinkedListNode head = new SinglyLinkedListNode();
        SinglyLinkedListNode tail = head;
        while (headA != null && headB != null) {
            if (headA.value <= headB.value) {
                tail.next = headA;
                headA = headA.next;
            } else {
                tail.next = headB;
                headB = headB.next;
            }
            tail = tail.next;
        }

View on GitHub (pinned to fdfb9a395b)

Solutions

  1. Pass a non-null empty SinglyLinkedList instead of null when a side has no data.
  2. Null-check both arguments before calling merge.
  3. Provide a single-argument convenience wrapper that substitutes an empty list for null.
  4. Enforce non-null list contracts at the data-source boundary.

Example fix

// before
SinglyLinkedList merged = MergeSortedSinglyLinkedList.merge(a, b); // b may be null

// after
SinglyLinkedList merged = MergeSortedSinglyLinkedList.merge(
    a != null ? a : new SinglyLinkedList(),
    b != null ? b : new SinglyLinkedList());
Defensive patterns

Strategy: validation

Validate before calling

if (listA != null && listB != null) {
    return MergeSortedSinglyLinkedList.merge(listA, listB);
}

Try / catch

try {
    return MergeSortedSinglyLinkedList.merge(listA, listB);
} catch (NullPointerException e) {
    // one input was null
}

Prevention

When it happens

Trigger: Passing null for listA or listB. A list reference from a map/optional that resolved to null. Merging where one side is conditionally populated.

Common situations: Optional.orElse(null) used for brevity. Branching that leaves one list null when its case is skipped. Deserialization producing null for an absent field.

Related errors


AI-assisted analysis of TheAlgorithms/Java@fdfb9a395b (2026-08-13). Data as JSON: /api/errors/d9760a8b311525fd. Report an issue: GitHub.