双重LinkedList Java

时间:2015-08-10 14:29:01

标签: java nullpointerexception doubly-linked-list

这是一个带有sortedAdded和sortedRemove方法的双向链表。当我运行代码时,它会抛出" null point exception"。我知道例外是在#34; 43"但我不知道如何解决它。我在这个网站上发了一篇关于如何防止零点异常的帖子"但仍然无法做到正确。这是我的代码:

public class DoublyLinkedList<Item extends Comparable> {

private class Node{
    private Item item;
    private Node next;
    private Node prev;

    public Node getNext() {
        return next;
    }

    public Node getPrev(){
        return prev;
    }

    public Item getItem(){
        return item;
    }

    public void setNext(Node next) {
        this.next = next;
    }

    public void setPrev(Node prev){
        this.prev= prev;
    }

    public void setItem(Item item){
        this.item = item;
    }
}

private Node head;
private int numberOfEelements;

public void sortedAdd(Item newItem) {
    Node dummy = head;
    Node current = dummy.getNext();
    while ((newItem.compareTo(current.getItem()) > 0) && (current != dummy))
        current = current.getNext();
    Node temp = new Node();
    temp.setNext(current);
    temp.setPrev(current.getPrev());
    (current.getPrev()).setNext(temp);
    current.setPrev(temp);
    temp.setItem(newItem);
    ++numberOfEelements;
}

public void sortedRemove(Item newItem) {
    Node dummy = head;
    Node current = dummy.getNext();
    while((newItem.compareTo(current.getItem()) !=0) && (current!= dummy))
        current = current.getNext();

    if (newItem.equals(current.getItem())) {
        (current.getPrev()).setNext(current.getNext());
        (current.getNext()).setPrev(current.getPrev());
        --numberOfEelements;
    }
}

public static void main(String[] args) {

    DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>();

        list.sortedAdd(1);
        list.sortedAdd(5);
        list.sortedAdd(7);
        list.sortedAdd(9);
        list.sortedAdd(3);
        list.sortedAdd(2);

        System.out.println(list);

        list.sortedRemove(3);
        list.sortedRemove(7);

        System.out.println(list);

        list.sortedRemove(4);
}
}

这是我得到的错误:

Exception in thread "main" java.lang.NullPointerException
    at DoublyLinkedList.sortedAdd(DoublyLinkedList.java:43)
    at DoublyLinkedList.main(DoublyLinkedList.java:74)

3 个答案:

答案 0 :(得分:4)

在sortedAdd中,您没有处理head == null的初始情况(在这种情况下这意味着一个空列表)。因此,在行dummy.getNext();中,您在空引用上调用getNext。

添加一个创建一个空头的检查应该会让你超过这个错误:

public void sortedAdd(Item newItem) {

    if (head == null) {
        head = new Node();
        head.setItem(newItem);
        ++numberOfEelements;
        return;
    }

虽然由于你的循环逻辑,你会再次击中另一个NPE。

要打印列表,您需要添加toString()方法:

public String toString(){
    Node current = head;
    StringBuilder sb = new StringBuilder();
    while(current != null){
        sb.append(current).append(" ");
        current = current.getNext();
    }
    return sb.toString();
}

答案 1 :(得分:1)

使用

创建Object类型DoublyLinkedList
DoublyLinkedList<Integer> list = new DoublyLinkedList<Integer>();

head初始化为nullnumberOfEelements初始化为0;

initializer课程中添加以下 DoublyLinkedList

   {
         head = new Node();
         head.setNext(head);
         head.setPrev(head);
         head.setItem(null);
         numberOfEelements =0;
    }
public class DoublyLinkedList<Item extends Comparable> {    
    {
      head = new Node();
      head.setNext(head);
      head.setPrev(head);
      head.setItem(null);
      numberOfEelements =0;
    }
 private class Node{
    private Item item;
    private Node next;
    private Node prev;

    public Node getNext() {
        return next;
    }

    public Node getPrev(){
        return prev;
    }

    public Item getItem(){
        return item;
    }

    public void setNext(Node next) {
        this.next = next;
    }


    public void setPrev(Node prev){
        this.prev= prev;
    }

    public void setItem(Item item){
        this.item = item;
    }
}

private Node head;
private int numberOfEelements;

public void sortedAdd(Item newItem) {
    Node dummy = head;
    Node current = dummy.getNext();
    while ((newItem.compareTo(current.getItem()) > 0) && (current != dummy))
        current = current.getNext();
    Node temp = new Node();
    temp.setNext(current);
    temp.setPrev(current.getPrev());
    (current.getPrev()).setNext(temp);
    current.setPrev(temp);
    temp.setItem(newItem);
    ++numberOfEelements;
}

public void sortedRemove(Item newItem) {
    Node dummy = head;
    Node current = dummy.getNext();
    while((newItem.compareTo(current.getItem()) !=0) && (current!= dummy))
        current = current.getNext();

    if (newItem.equals(current.getItem())) {
        (current.getPrev()).setNext(current.getNext());
        (current.getNext()).setPrev(current.getPrev());
        --numberOfEelements;
    }
}

答案 2 :(得分:0)

首先,当您在类Node结束后初始化head时 - 将该初始化替换为:private Node head=null;

现在使用以下代码:

public void sortedAdd(Item newItem) {
    Node dummy = head;
    if(dummy==null)
    {
        head = new Node();
        head.setItem(newItem);
        head.setNext(null);
        head.setPrev(head);

    }
    else
    {
        Node current = dummy.getNext();
        while ((newItem.compareTo(current.getItem()) > 0) && (current != dummy))
            current = current.getNext();
        Node temp = new Node();
        temp.setNext(current);
        temp.setPrev(current.getPrev());
        (current.getPrev()).setNext(temp);
        current.setPrev(temp);
        temp.setItem(newItem);
    }
    ++numberOfEelements;

}