-1

Here's my Node object:

public class Node<Any> {

protected Any data;
protected Node<Any> link;

public Node() {
    this.data=null;
    this.link=null;
}

public Node(Any data, Node<Any> link) {
    this.data=data;
    this.link=link;
}

public void setData(Any data) { 
    this.data=data;
}

public void setLink(Node<Any> link) {
    this.link=link;
}

public Any getData() {
    return this.data;
}

public Node<Any> getLink() {
    return this.link;
}

public String toString() {
    return "Data: "+this.data+" | Link: "+this.link;
}
}

And here's my SinglyLinkedList object:

public class SinglyLinkedList<Any> {
private Node<Any> head;
private Node<Any> tail;
private int size;

public SinglyLinkedList() {
    this.head=null;
    this.tail=null;
    this.size=0;
}

//overloaded constructor for array to be added here

public void insertAsHead(Any thing) {
    Node<Any> tmp=new Node<Any>(); //Create new node
    tmp.data=thing; //new_node->node.data=new_value
    tmp.link=this.head; //new_node->node.link=head
    this.head=tmp; //head=new_node
    size++;
}

public void insertAsTail(Any thing) {
    if(head==null) 
        insertAsHead(thing);
    else {
        Node<Any> tmp = new Node<Any>(); //Create new node
        tmp.data=thing; //new_node->node.data=new_value
        tmp.link=null; //new_node->node.link=null
        this.tail.link=tmp; //tail->node.link=new_node
        this.tail=tmp; //tail=new_node;
    }
    size++;
}

public void insertBefore(int i, Any thing) {

}

public void insertAfter(int i, Any thing) {

}

public void insertAt(int i, Any thing) {

}

public void deleteHead() {
    size--;
}

public void deleteTail() {
    size--;
}

public void deleteAt(int i) {
    size--;
}

public Any retrieve(int i) {
    return null;
}

public void set(int i, Any thing) {

}

public boolean isEmpty() {
    if(this.size==0)
        return true;
    else
        return false;
}

public int size() {
    return this.size;
}

public String toString() {
    String s="["+this.head.data;
    Node<Any> next = this.head.link;
    while(next!=null) {
        s+=", "+next.data;
        next=next.link;
    }
    return s+"]";
}
}

Whenever I use the method 'insertAsTail(x)' when linked list contains values, the program execution halts and says that there is a NullPointerException at the line this.tail.link=tmp; //tail->node.link=new_node. I have tried going for the this.tail.setLink(tmp) route but same exception occurs.

flakes
  • 21,558
  • 8
  • 41
  • 88
ArcIX
  • 35
  • 4
  • I forgot to mention it's not complete yet so excuse the emptiness in several parts of the code. – ArcIX Nov 15 '16 at 14:20
  • 3
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – khelwood Nov 15 '16 at 14:26
  • That means that ``this.tail`` is null. And why wouldn't it be ``null`` when the list is empty? – f1sh Nov 15 '16 at 14:27
  • You insert first element using `insertAsHead` which never sets the tail, so it's null. With one element you should have `head == tail`. – Jaroslaw Pawlak Nov 15 '16 at 14:28
  • @Jaroslaw Sir, I love you. I did not realize I had to set the head as the tail if ony one element exists in the list. Thank you very much! P.S. I am surprised by the response rate of this question. – ArcIX Nov 15 '16 at 14:44

3 Answers3

0

You really need to learn how to use the debugger. However, you issue is with:

this.tail.link

this.tail is null here.

Joe
  • 800
  • 4
  • 10
  • 26
0

Your code is very much non understandable. For inserting at the end you just need to check if head is present or not. If not create a new node and assign as head.

If head is already present you need to traverse till the end of the list. And then you need to add the node.

I cannot see you traversing the list until end.

Pseudocode :

public void addAtEnd(int new_data)
{
    Node new_node = new Node(new_data);
    if (head == null){
        head = new Node(new_data);
        return;
    }
    new_node.next = null;
    Node last = head; 
    while (last.next != null)
        last = last.next;
    last.next = new_node;
    return;
}
FallAndLearn
  • 4,035
  • 1
  • 18
  • 24
  • I am sorry. I am aware it's not quite understandable but I'm trying to keep the structure of the logic as close to what my Data Structure professor has taught me. I WAS starting to consider the traverse-the-whole-list method. Thanks still, good sir/ma'am. – ArcIX Nov 15 '16 at 14:52
0

Looks like your (linkedlist) tail was never initialized. for the first element as the head, you need to denote the same element as tail.

public void insertAsHead(Any thing) {
    Node<Any> tmp=new Node<Any>(); //Create new node
    tmp.data=thing; //new_node->node.data=new_value
    tmp.link=this.head; //new_node->node.link=head
    this.head=tmp; //head=new_node
    if(this.size == 0){
     this.tail=this.head;
    }
    size++;
} 

Basically when you add first element, it's head and tail both because it's the only element.

Rahul Kumar
  • 2,781
  • 1
  • 21
  • 29