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++;
2
  • Seems like it was the head.setPrev(n). Should that be set to null instead? Commented Mar 6, 2012 at 1:09
  • Ahhhh it should be n.setPrev() XD Commented Mar 6, 2012 at 1:10

1 Answer 1

1

Well, this seems like the most likely fix. Initialize the head to null, and add a condition for when the list is empty.

public AddressList() {
    head = null
}

And your condition would look like this:

if (head == null) {  // empty list
    head = n
}
else {
    head.setPrev(n);
    n.setNext(head);
    head = n;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.