我创建了一个从链表中删除节点的函数,但它给了我一个NullPointerException。我试图检查下一个是否为空,但它现在给我那个错误。尝试从链接列表中删除节点时出现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;
}
Node类:
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();
}
}
你在哪里创建'aNode' – Prateek
什么类是部分的removeNode?你指的是什么? – Andrew
可能无法解决问题,但我会说在使用它之前检查aNode是否为空是个好主意。 – ahansen