0

So I'm implementing a Singly LinkedList in Java. However, my addLast() method kept throwing a NullPointerException.

Here is the error message:

Exception in thread "main" java.lang.NullPointerException: Cannot assign field "next" because "list.tail" is null at SinglyLinkedList.SLinkedList.addLast(SLinkedList.java:39) at SinglyLinkedList.SLinkedList.main(SLinkedList.java:98)

Process finished with exit code 1


I don't understand why tail is null. Any help would be appreciated.

Here is my code

public class SLinkedList {
    private SNode head;
    private SNode tail;
    private static int size;

    private static class SNode {
        String name;
        SNode next;

        //constructor
        SNode(String name) {
            this.name = name;
            next = null;
        }
    }

    public static SLinkedList insertNode(SLinkedList list, String name) {
        SNode newNode = new SNode(name);
        newNode.next = null;

        if (list.head == null) {
            list.head = newNode;
        } else {
            SNode last = list.head;
            while (last.next != null) {
                last = last.next;
            }
            last,next = newNode;
        }
        size++;
        return list;
    }

    public static SLinkedList addLast(SLinkedList list, String name){
        SNode newNode = new SNode(name);

        list.tail.next = newNode;
        list.tail = list.tail.next;

        size++;
        //return the list
        return list;
    }

    // a method that add head to the list
    public static SLinkedList addFirst(SLinkedList list, String input) {
        SNode newNode = new SNode(input);
        newNode.next = list.head;
        if (list.head == null) {
            list.tail = newNode;
        }
        list.head = newNode;
        size++;
        return list;
    }

    //a method to remove the head of the list
    public static String removeFirst(SLinkedList list) throws Exception {
        SNode temp = list.head;
        // edge cases
        // size 0
        if (size == 0) {
            throw new Exception("The LinkedList is empty.");
        }

        list.head = temp.next;
        temp.next = null;
        size--;

        // edge cases
        // size 0
        if (size == 0) {
            list.tail = null;
        }

        return temp.name;
    }

    public static void printSList(SLinkedList list) {
        SNode current = list.head;
        System.out.print("Singly LinkedList is: ");

        while (current != null) {
            System.out.print(current.name + " ");
            current = current.next;
        }
    }

    //driver code
    public static void main(String[] args) throws Exception {
        SLinkedList list = new SLinkedList();

        insertNode(list, "nuggie");
        insertNode(list, "cloud");

        addLast(list, "cotton");
        // addLast(list, "jamie");
        // addLast(list, "pot-roast");

        addFirst(list, "kennedy");
        addFirst(list, "hoegaarden");

        System.out.println("The element removed is " + removeFirst(list));

        System.out.println("Size of the Singly Linked List is " + size);
        printSList(list);
    }

}
john doe
  • 13
  • 4

1 Answers1

0

The tail element of your linked list has not been initialized. Inside insertNode you need to update your tail:

if (list.head == null) {
    list.head = newNode;    
} else {
    SNode last = list.head;
    while (last.next != null) {
        last = last.next;
    }
    last.next = newNode;
}
list.tail = newNode; // add this
Gautham M
  • 4,816
  • 3
  • 15
  • 37