尝试从链表中删除节点时是否出现NullPointerException?

时间:2021-08-20 23:09:23

I'm creating a function that removes a node from a linked list but it is giving me a NullPointerException. I tried to check to see if the next one is null but it is giving me that error there now.

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

Remove function:

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

Node class:

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

Here is the main section of the full Linked List class

这是完整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();
    }

}

3 个解决方案

#1


3  

You get that exception because aNode is null and you try to call a null object's getNext() method, which means at some point, you called remove(null). Since you don't show us where you call remove(), it is impossible to tell, but you either need to make sure that doesn't happen, or check explicitly if aNode is null before attempting to call methods on it.

您得到该异常是因为aNode为null并且您尝试调用null对象的getNext()方法,这意味着在某些时候,您调用了remove(null)。由于您没有向我们显示您调用remove()的位置,因此无法判断,但是您需要确保不会发生这种情况,或者在尝试调用方法之前显式检查aNode是否为null。

If you aren't expecting aNode to be null but it is, you should double check your code to make sure you are actually implementing everything properly, as this is a good indication that something is going wrong elsewhere in your algorithm.

如果你不期望aNode为null但是它应该是,你应该仔细检查你的代码,以确保你实际上正确地实现了所有内容,因为这是一个很好的迹象表明你的算法中的其他地方出了问题。

Update (looking at your edited question with new code): You have:

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

success = linkedList.remove(null);

That is the source of your problem; my above answer covers your options for fixing the exception.

这是你问题的根源;我的上述答案涵盖了修复例外的选项。

In the future you need to examine (and post) the entire stack trace of your exception, which would clearly identify that line of code.

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

#2


1  

You must be calling remove with aNode set to null. There is no other explanation for this behavior.

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

It is good practise to assert aNode != null if you do not expect it to be.

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

#3


1  

It could only mean that aNode itself is null

它只能意味着aNode本身为null

#1


3  

You get that exception because aNode is null and you try to call a null object's getNext() method, which means at some point, you called remove(null). Since you don't show us where you call remove(), it is impossible to tell, but you either need to make sure that doesn't happen, or check explicitly if aNode is null before attempting to call methods on it.

您得到该异常是因为aNode为null并且您尝试调用null对象的getNext()方法,这意味着在某些时候,您调用了remove(null)。由于您没有向我们显示您调用remove()的位置,因此无法判断,但是您需要确保不会发生这种情况,或者在尝试调用方法之前显式检查aNode是否为null。

If you aren't expecting aNode to be null but it is, you should double check your code to make sure you are actually implementing everything properly, as this is a good indication that something is going wrong elsewhere in your algorithm.

如果你不期望aNode为null但是它应该是,你应该仔细检查你的代码,以确保你实际上正确地实现了所有内容,因为这是一个很好的迹象表明你的算法中的其他地方出了问题。

Update (looking at your edited question with new code): You have:

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

success = linkedList.remove(null);

That is the source of your problem; my above answer covers your options for fixing the exception.

这是你问题的根源;我的上述答案涵盖了修复例外的选项。

In the future you need to examine (and post) the entire stack trace of your exception, which would clearly identify that line of code.

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

#2


1  

You must be calling remove with aNode set to null. There is no other explanation for this behavior.

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

It is good practise to assert aNode != null if you do not expect it to be.

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

#3


1  

It could only mean that aNode itself is null

它只能意味着aNode本身为null