在我可以访问我的代码之前,我得到了一个空指针异常。我无法判断它是否会正常工作,因为我无法让它通过这一点。
此行中抛出错误:
if(currentNode.getData() > currentNode.getNext().getData())
这是我的冒泡排序:
public static void bubbleSort(DoubleLinkedList list) //static method used to sort the linked list using bubble sort
{
int i = 0;
int j = 0;
Node currentNode = list.head;
Node previousNode = currentNode;
Node tempNext = currentNode;
Node tempPrevious = currentNode;
for(i=1; i<list.getSize(); i++)
{
for(j=0; j<list.getSize()-1; j++)
{
if(currentNode.getData() > currentNode.getNext().getData())
{
if(currentNode == list.head)
{
Node tempNode = currentNode.getNext();
list.head = tempNode;
tempNext = tempNode.getNext();
tempNode.setNext(currentNode);
currentNode.setNext(tempNext);
currentNode.setPrevious(tempNode);
tempNext.setPrevious(currentNode);
}
else if(currentNode.getNext() == list.last)
{
Node tempNode = currentNode.getNext();
list.last = currentNode;
tempPrevious = currentNode.getPrevious();
tempNode.setNext(currentNode);
tempNode.setPrevious(tempPrevious);
currentNode.setPrevious(tempNode);
}
else
{
Node tempNode = currentNode.getNext();
tempPrevious = currentNode.getPrevious();
tempNext = currentNode.getNext();
tempPrevious.setNext(tempNode);
tempNext.setPrevious(currentNode);
currentNode.setNext(tempNext);
tempNode.setPrevious(tempPrevious);
}
}
currentNode = currentNode.getNext();
}
}
}
我认为对循环的限制将使编译器无法尝试访问不一定存在的指针,这就是我理解的空指针异常。
如果有人可以帮助我克服这个错误,以便我可以测试我的冒泡排序,我将非常感激!