0

I'm getting this exception which seemed to be that I was doing things with a node that was null. Can someone explain how I am doing that? What is the constructor supposed to look like? I've seen it empty or with header and trailer dummy nodes..

    //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++;
4

1 に答える 1

1

まあ、これは最も可能性の高い修正のようです。ヘッドを null に初期化し、リストが空の場合の条件を追加します。

public AddressList() {
    head = null
}

そして、あなたの状態は次のようになります:

if (head == null) {  // empty list
    head = n
}
else {
    head.setPrev(n);
    n.setNext(head);
    head = n;
}
于 2012-03-06T01:17:15.463 に答える