コードに到達する前に、nullポインタ例外が発生します。この点を通過させることができないため、正しく機能するかどうかわかりません。
次の行にエラーがスローされます。
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();
}
}
}
ループの制限により、コンパイラが必ずしも存在しないポインタにアクセスしようとするのを防ぐことができると思いました。これは、nullポインタ例外であると私が理解していることです。
誰かが私がこのエラーを乗り越えてバブルソートをテストできるように助けてくれたら、私は非常に感謝しています!