0

I've made a Singly Linked List but I keep getting a NullPointerException. The insert method is supposed to add an Object to Singly Linked List then I take all the elements of the SLL and put them into MyVector class where I then use my quick sort algorythm I made for MyVector class then put the Objects back into the SSL. I'm not totally sure why I keep getting the error.

Exception in thread "main" java.lang.NullPointerException at java.lang.Integer.compareTo(Integer.java:978) at java.lang.Integer.compareTo(Integer.java:37) at collection.SortedSLList.remove(SortedSLList.java:51) at collection.SortedSLList.insert(SortedSLList.java:39) at lab.Lab6.test(Lab6.java:15) at main.Main.main(Main.java:15) Java Result: 1

public void insert(Object element) {
    if(head == null) {
        head = tail = new SLListNode(element, null);
        ++size;
        return;
    }
    tail = tail.next = new SLListNode(element, null);
    ++size;
    MyVector temp = new MyVector();
    int i = size;
    Object t = head.data;
    while(temp.size() < i) {
        temp.append(t);
        remove(t); //this line
        t = head.next;
    }
    MySort.quickSort(temp);
    i = 0;
    while(size < temp.size()) {
        insert(temp.elementAt(0));
        ++i;
    }
}
public boolean remove(Object element) {
    if(head == null) return false;
    if(((Comparable)(head.data)).compareTo(element) == 0) { //this line
        if(head == tail) {
            head = tail = null;
            return true;
        }
        head = head.next;
        return true;
    }
    if(head == tail) return false;
    SLListNode ref = head;
    while(ref.next != tail) {
        if(((Comparable)(ref.next.data)).compareTo(element) == 0) {
            ref.next = ref.next.next;
            return true;
        }
        ref = ref.next;
    }
    if(((Comparable)(tail.data)).compareTo(element) == 0) {
        tail = ref;
        tail.next = null;
        return true;
    }
    return false;
}
4
  • 2
    can you post your stacktrace please, or let us know on what line the error is Commented Nov 4, 2012 at 19:42
  • Not sure what stacktrace is but I marked the lines in the code and added the error message that NetBeans gives me. Commented Nov 4, 2012 at 19:45
  • the exception which you posted is the stacktrace ..:) Commented Nov 4, 2012 at 19:47
  • Is that a recursive call to insert? How does that work? Commented Nov 4, 2012 at 19:57

2 Answers 2

1

The exception trace says that you are calling remove(null). For some reason, head.data or head.next contains null. I suggest that you add a printout here:

Object t = head.data;
while(temp.size() < i) {
    System.out.println("Looking at " + t); // <-- add here
    temp.append(t);
    remove(t); //this line
    t = head.next;
}

Then watch what those values are doing. You'll see that one of them comes up null.

Sign up to request clarification or add additional context in comments.

Comments

0

The problem is where you do:

while(temp.size() < i) {
    temp.append(t);
    remove(t); //this line
    t = head.next;
}

The problem is that you have removed the head (t), so you should be setting t equal to head.data, not head.next.

1 Comment

Actually I guess it should be head.data since you're deleting the data, not the node.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.