Java 双重链接列表-再次出现空指针异常

Java 双重链接列表-再次出现空指针异常,java,doubly-linked-list,Java,Doubly Linked List,我得到了一个异常,它似乎是我在用一个空节点做事情。有人能解释一下我是怎么做的吗?构造函数应该是什么样子的?我看到它是空的,或者带有头节点和尾节点 //default constructor public AddressList() { } //get size public int size() { return counter; } public void addEntryNode(){ //create the new node EntryNod

我得到了一个异常,它似乎是我在用一个空节点做事情。有人能解释一下我是怎么做的吗?构造函数应该是什么样子的?我看到它是空的,或者带有头节点和尾节点

    //default constructor
public AddressList() {

}

    //get size
public int size() {
    return counter;
}

public void addEntryNode(){
    //create the new node
    EntryNode n = new EntryNode();
    //set the data
    System.out.println("Enter first name: ");
    n.myFirstName = keyboard.next();
    n.setFirstName(n.myFirstName);

    System.out.println("Enter Last Name: ");
    n.myLastName = keyboard.next();
    n.setLastName(n.myLastName);

    System.out.println("Enter Email Address: ");
    n.myEmail = keyboard.next();
    n.setEmail(n.myEmail);

    System.out.println("Enter Phone Number: ");
    n.myPhoneNum = keyboard.next();
    n.setPhoneNum(n.myPhoneNum);

    n.setIndex(index);

    //add nodes to head of list
    head.setPrev(n);
    n.setNext(head);
    head = n;

    //up the count and index 
    counter++;

嗯,这似乎是最有可能的解决办法。将head初始化为null,并添加列表为空时的条件

public AddressList() {
    head = null
}
你的情况是这样的:

if (head == null) {  // empty list
    head = n
}
else {
    head.setPrev(n);
    n.setNext(head);
    head = n;
}

好像是头。setPrev(n)。应该改为设置为null吗?啊,它应该是n.setPrev()XD