La lista enlazada ordenada lexográficamente devuelve una excepción de puntero nulo

I'm trying to build a lexicographically ordered linked list. I planned it all out on paper and thought I need everything right, but once I insert a second or third entry it returns with a null pointer exception.

Exception in thread "main" java.lang.NullPointerException
at DoublyLL.insert(DoublyLL.java:88)

If I enter, for examples:

"chris", "andy", then "bob", "bob" returns the excpetion.
"chris", "bob", then "andy", "andy" returns the exception
"andy", "bob", I get the same exception with the addition of at DoublyLL$Node.access$000(DoublyLL.java:148)

El código:

public boolean insert(StudentListing newListing)
{  Node n = new Node();
    if(n == null) // out of memory
       return false;
    else
    {
                    Node q = new Node();
        q = h;
        int lex;
        if (q.next == null)     // first inputed node
        {
            n.next = q.next;
            q.next = n;
            n.back = q;
            n.l = newListing.deepCopy();
            return true;
        } else                  // not first node
        {
            q = q.next;
            do
            {
               // This is the line the error is called vvv
                lex = q.l.getKey().compareTo(newListing.getKey());
               // ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
                if (lex < 0)
                {
                    // keep going
                    q = q.next;
                } else if (lex > 0)
                {
                    // place before q
                    n.back = q.back;
                    q.back = n;
                    n.next = q;
                    n.back.next = n;
                    return true;
                } else if (lex == 0)
                {
                    // q and listing match
                }
            } while (lex < 0);
        }
    }
    return false;
}

Inner class

 public class Node
 {   private StudentListing l;
     private Node next;
     private Node back;
     public Node()
 { }
}

preguntado el 10 de marzo de 12 a las 14:03

1 Respuestas

The biggest problem here is that, when you're inserting a new StudentListing to a non-empty list, you iterate over the list until you find an element that's greater than the StudentListing you're inserting. But if the StudentListing that you're inserting is greater than any element in the list, then you never find such an element, so you run off the end of the list. Before writing q = q.next, necesitas comprobar si q.next == null, and handle that case appropriately.

(There are also various little non-Java-isms in your code — for example, if(n == null) // out of memory will never be true, because Java indicates an out-of-memory error by raising an exception rather than by returning null — but none of those looks like a major problem to me.)

respondido 10 mar '12, 15:03

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