単一リンク リストを作成しましたが、NullPointerException が引き続き発生します。挿入メソッドは、Singly Linked List にオブジェクトを追加することになっています。次に、SLL のすべての要素を取得して MyVector クラスに配置し、MyVector クラス用に作成したクイック ソート アルゴリズムを使用して、オブジェクトを SSL に戻します。なぜエラーが発生し続けるのか、完全にはわかりません。
スレッド「メイン」で例外 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) で collection.SortedSLList.insert(SortedSLList.java:39) で lab.Lab6.test(Lab6.java:15) で main.Main.main(Main.java:15) Java 結果: 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;
}