-1

我正在创建一个从链表中删除节点的函数,但它给了我一个 NullPointerException。我试图检查下一个是否为空,但现在它给了我那个错误。

删除功能:

 private boolean remove(Node aNode)
    {
        Node prevNode, nextNode;
        prevNode = this.getPrevious(aNode);
        if(aNode.getNext()==null){ // NullPointerException
            return false;
        }
        else{
            nextNode = aNode.getNext();
            prevNode.setNext(nextNode);
        }

        return false;
    }

节点类:

public class Node
{
    ///////////////////////////////////
    //           Properties          //
    ///////////////////////////////////
    private Object myData;
    private Node myNext;

    ///////////////////////////////////
    //             Methods           //
    ///////////////////////////////////

    /**
     *  Default constructor for a node with null
     *  data and pointer to a next node
     */
    public Node()
    {
        myData = null;
        myNext = null;
    }

    /**
     *  Constructor for a node with some object for
     *  its data and null for a pointer to a next node
     *
     *  <pre>
     *  pre:  a null node
     *  post: a node with some object for its data and
     *        null for a pointer to a next node
     *  </pre>
     *
     *  @param datum an object for the node's data
     */
    public Node(Object datum)
    {
        myData = datum;
        myNext = null;
    }

    /**
     *  Constructor for a node with some object for 
     *  its data and a pointer to another node
     *
     *  <pre>
     *  pre:  a null node
     *  post: a node with some object for its data and
     *        a pointer to a next node
     *  </pre>
     *
     *  @param datum an object for the node's data
     *  @param next the node that this node points to
     */
    public Node(Object datum, Node next)
    {
        myData = datum;
        myNext = next;
    }

    // Accessor methods
    public void setData(Object datum)
    {
        myData = datum;
    }

    public Object getData()
    {
        return myData;
    }

    public void setNext(Node next)
    {
        myNext = next;
    }

    public Node getNext()
    {
        return myNext;
    }
}

这是完整的链表类的主要部分

public static void main(String[] args)
    {
        LinkedList linkedList;
        Node testNode1, testNode2, testNode10, foundNode;
        boolean success;

        linkedList = new LinkedList();

        // Test "inList()" method
        testNode1 = new Node(new Integer(1));
        testNode2 = new Node(new Integer(2));
        testNode10 = new Node(new Integer(10));

       // System.out.println("In List = "+linkedList.inList(null));
        linkedList.printList();
        foundNode = linkedList.findNode(new Integer(2));
        System.out.println("Found node "+foundNode);
        success = linkedList.remove(null);
        System.out.println("Success = "+success);
        success = linkedList.remove(testNode1);
        System.out.println("Success = "+success);
        linkedList.addFirst(testNode1);
        success = linkedList.remove(testNode1);
        System.out.println("Success = "+success);
        linkedList.printList();
       // System.out.println("In List = "+linkedList.inList(null));
       // System.out.println("In List = "+linkedList.inList(testNode1));
       // System.out.println("In List = "+linkedList.inList(testNode2));

        // Test "addLast()" and "addFirst()" methods
        linkedList.addLast(new Node(new Integer(1)));
        linkedList.addLast(new Node(new Integer(2)));
        linkedList.addLast(new Node(new Integer(3)));
        linkedList.addLast(testNode10);
        foundNode = linkedList.findNode(new Integer(2));
        System.out.println("Found node "+foundNode.toString());
        linkedList.printList();

        Node testNode;
        testNode = linkedList.getPrevious(foundNode);
        System.out.println(testNode.getData());
        System.exit(0);

        success = linkedList.insertBefore("H", testNode1);
        System.out.println("Success = "+success);
        linkedList.printList();
        linkedList.addFirst(new Node(new Integer(1)));
        linkedList.addFirst(new Node(new Integer(2)));
        linkedList.addFirst(new Node(new Integer(3)));
        linkedList.printList();
        success = linkedList.insertBefore("A", testNode10);
        System.out.println("Success = "+success);
        linkedList.printList();

        // Test "remove()"
        success = linkedList.remove(testNode1);
        System.out.println("Success = "+success);
        success = linkedList.remove(testNode2);
        System.out.println("Success = "+success);
        success = linkedList.remove(testNode10);
        System.out.println("Success = "+success);
        linkedList.printList();
    }

}
4

3 回答 3

3

你得到那个异常是因为aNodeisnull并且你试图调用一个null对象的getNext()方法,这意味着在某些时候,你调用了remove(null). 由于您没有向我们显示您调用的位置remove(),因此无法分辨,但您要么需要确保不会发生这种情况,要么在尝试对其调用方法之前明确检查 if aNodeis 。null

如果您不希望 aNode如此null,那么您应该仔细检查您的代码以确保您实际上正确地实现了所有内容,因为这很好地表明您的算法中的其他地方出现了问题。

更新(用新代码查看您编辑的问题):您有:

success = linkedList.remove(null);

那是您问题的根源;我上面的答案涵盖了您修复异常的选项。

将来您需要检查(并发布)异常的整个堆栈跟踪,这将清楚地识别该代码行。

于 2013-11-14T21:53:53.013 回答
1

您必须在 aNode 设置为 null 的情况下调用 remove。这种行为没有其他解释。

如果您不期望它是断言 aNode != null 是一个好习惯。

于 2013-11-14T21:57:55.820 回答
1

它只能意味着它aNode本身是空的

于 2013-11-14T21:59:12.680 回答