簡體   English   中英

我的LinkedList中的Nullpointer異常

[英]Nullpointer exception in my LinkedList

我有一個List類,其中有此插入函數。 列表正在對列表中的數字進行遞減排序。 當我插入以下語句Ape ={ 1, 2, 3, 4, 2, 3, 4, 5, 3, 4, 6 } ,出現空指針異常。

public List<E> insert (E d){
    Node<E> newNode= new Node<E>(d);
    if(isEmpty()){
        current=first=last=newNode;
        numberOfNodes++;
        return this;
    }

    if(newNode.data.compareTo(first.data)>0){
        first.prior=newNode;
        newNode.next=first;
        first= current = newNode;
        numberOfNodes++;
        return this;
    }

    setFirst();
    while(current.next != null && newNode.data.compareTo(current.data)<0){
        current=current.next;
    }
    if(current.next==null){
        current.next=newNode;
        newNode.prior=current;
        last=current=newNode;
        numberOfNodes++;
        return this;
    }
    else{
        current.prior.next=newNode;
        newNode.prior=current.prior;
        newNode.next=current;
        current.prior=newNode;
        current=newNode;
        numberOfNodes++;
        return this;
    }
}

我在current.prior.next = newNode處收到一個nullpointer異常;

有人可以幫我弄這個嗎?

非常感謝!

null元素實際上必須是current.prior如果current為null,則在前面的if處應該有一個nullpointer。 這意味着您正在查看的節點沒有先前的節點。 當您逐步執行代碼時,這將變得顯而易見。

也:

  • Java中實際上有一個LinkedList類,為什么不使用它
    代替?
  • 請不要執行諸如current=first=last=newNode;類的鏈接分配current=first=last=newNode;
  • 您是否真的需要所有這些字段都成為成員字段?
  • 添加一些JavaDoc或將此方法划分為子方法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM