gpt4 book ai didi

java - 我不知道如何修复错误 NullPointerException

转载 作者:行者123 更新时间:2023-12-01 15:03:47 25 4
gpt4 key购买 nike

我认为 smallIndexindextemp 都有值,所以我不确定为什么会收到错误。谁能向我解释为什么会发生这种情况?错误信息是:

线程“main”中的异常 java.lang.NullPointerException

public class LinkedList
{
public class LinkedListNode
{
public int info;
public LinkedListNode next;
public LinkedListNode back;

public LinkedListNode()
{
info = 0;
next = null;
back = null;
}
public LinkedListNode(int item)
{
info = item;
next = null;
back = null;
}
public void displayInfo()
{
System.out.print(info + " ");
}
}
protected int count;
protected LinkedListNode first;
protected LinkedListNode last;

public LinkedList()
{
first = null;
last = null;
count = 0;
}
public void initializeList()
{
first = null;
last = null;
count = 0;
}
public boolean isEmpty()
{
return (first == null);
}
public int length()
{
return count;
}
public void print()
{
LinkedListNode current = first;
while (current != null)
{
current.displayInfo();
current = current.next;
}
}
public void insertNode(int insertItem)
{
LinkedListNode newNode = new LinkedListNode(insertItem);
if (isEmpty())
{
first = newNode;
last = newNode;
count++;
}
else
{
last.next = newNode;
newNode.back = last;
}
last = newNode;
}
public LinkedListNode partition(LinkedList list,
LinkedListNode first, LinkedListNode last)
{
LinkedListNode smallIndex = first;
LinkedListNode index = smallIndex.next;
LinkedListNode temp = new LinkedListNode();
int pivot = first.info;

while (index != last.next)
{
if((index.info) < pivot)
{
smallIndex = smallIndex.next;
temp.info = index.info;
index.info = smallIndex.info;
smallIndex.info = temp.info;
}
index = index.next;
}
temp.info = first.info;
first.info = smallIndex.info;
smallIndex.info = temp.info;
System.out.print("The list after QuickSort is: ");
list.print();
System.out.print("\n");
return smallIndex;
}
public void recQuickSort(LinkedList list, LinkedListNode first,
LinkedListNode last)
{
while(first != last)
{
LinkedListNode pivotLocation = partition(list, first, last);
recQuickSort(list, first, pivotLocation.back);
recQuickSort(list, pivotLocation.next, last);
}
}
public void quickSortLinkedList(LinkedList list)
{
recQuickSort(list, list.first, list.last);
}

}



import java.util.*;

public class testLinkedListQuickSort
{
static Scanner console = new Scanner(System.in);

public static void main(String[] args)
{
LinkedList linkedlist = new LinkedList();
int num;

System.out.println("Enter numbers to add to linked list:");
num = console.nextInt();
while (num != 0)
{
linkedlist.insertNode(num);
num = console.nextInt();
}
linkedlist.quickSortLinkedList(linkedlist);
linkedlist.print();
}
}

最佳答案

关于您的评论:您调用分区

recQuickSort(list, first, pivotLocation.back);

如果 pivotLocation.back 就这样做是 null然后用 last == null 调用分区方法这会导致你的 NPE。

关于java - 我不知道如何修复错误 NullPointerException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13263601/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com