我正在创建一个从链表中删除节点的函数,但它给了我一个 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();
}
}