双链表 - 添加方法获取NPE

时间:2012-03-09 13:41:43

标签: java doubly-linked-list

我在添加到空列表时获得NPE。我不知道有什么问题。我已初始化head = null和tail = null然后我正在检查head == null,然后列表必须为空,所以添加到头部。由于它是列表中唯一的节点,因此它必须指向null并且head = newnode和tail = newnode。右??

    public AddressList() {
    head = null;
    tail = null;
}
public void addEntry(String firstName, String lastName, String phoneNum, String email) {

    EntryNode n = new EntryNode();

    if (head == null) {
        System.out.println("List is empty ");
        n.setNext(null);
        n.setPrev(null);
        tail = n;
        head = n;
    }
    else {
        //add to the head
        head.setPrev(n);
        n.setNext(head);
        head = n;
    }
    n.setFirstName(firstName);
    n.setLastName(lastName);
    n.setPhoneNum(phoneNum);
    n.setEmail(email);

    size++;
}

0 个答案:

没有答案