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;
}