-1

リンク リストからノードを削除する関数を作成していますが、NullPointerException が発生します。次のものがnullかどうかを確認しようとしましたが、現在そのエラーが発生しています。

削除機能:

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

完全な Linked List クラスのメイン セクションは次のとおりです。

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

aNodeis が原因でその例外が発生し、オブジェクトのメソッドnullを呼び出そうとします。つまり、ある時点で を呼び出しました。を呼び出す場所が表示されないため、特定することはできませんが、メソッドを呼び出そうとする前に、それが起こらないことを確認するか、明示的に確認する必要があります。nullgetNext()remove(null)remove()aNodenull

期待 aNodeしていなかっnullたが実際にそうである場合は、コードを再確認して、実際にすべてが適切に実装されていることを確認する必要があります。これは、アルゴリズムの他の場所で何か問題が発生していることを示す良い兆候です。

更新(新しいコードで編集した質問を見てください):あなたは持っています:

success = linkedList.remove(null);

それが問題の原因です。上記の回答は、例外を修正するためのオプションをカバーしています。

将来的には、例外のスタック トレース全体を調査 (および投稿) する必要があります。これにより、そのコード行が明確に識別されます。

于 2013-11-14T21:53:53.013 に答える
1

aNode を null に設定して remove を呼び出す必要があります。この動作について他に説明はありません。

予期しない場合は、aNode != null をアサートすることをお勧めします。

于 2013-11-14T21:57:55.820 に答える