我正在创建一个从链表中删除节点的函数,但它给了我一个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();
}
}
答案 0 :(得分:3)
您获得该异常是因为aNode
为null
并且您尝试调用null
对象的getNext()
方法,这意味着您在某个时候调用{{1} }。由于您没有告诉我们您拨打remove(null)
的位置,因此无法确定,但您需要确保不会发生这种情况,或明确检查remove()
是aNode
在尝试调用方法之前。
如果您期待 null
不是aNode
但是它是,您应该仔细检查您的代码,以确保您实际上正确实施所有内容,因为这是一个很好的迹象,表明算法中的其他地方出了问题。
更新(使用新代码查看已编辑的问题):您有:
null
这是你问题的根源;我的上述答案涵盖了修复例外的选项。
将来您需要检查(并发布)异常的整个堆栈跟踪,这样可以清楚地识别该行代码。
答案 1 :(得分:1)
您必须在aNode设置为null的情况下调用remove。这种行为没有其他解释。
优良作法是断言aNode!= null如果您不期望它。
答案 2 :(得分:1)
这可能只意味着aNode
本身为空