Lista doblemente vinculada - Agregar método get NPE

I get a NPE while adding to an empty list. I do NOT see what is wrong though. I've initialized head= null and tail = null and then I'm checking to see if head == null, then the list must be empty, so add to the head. Since it's the only node in the list, it's next and prev must point to null and head=newnode and tail=newnode. Right??

    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++;
}

preguntado el 09 de marzo de 12 a las 13:03

And where is your exception being thrown? -

Podrías mostrar EntryNode ¿por favor? -

Yikes, just realized it's being thrown in a completely different place. I wanted a switch case of Strings so I implemented it with enums but it seems a bit wacky. -

Provide us the stack trace from the exception? It looks like your problem isn't in that method. -

@JackieAldama Switch on Strings has been introduced in Java 7 if you can use that version. -

0 Respuestas

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.