gpt4 book ai didi

java - NullPointerException 问题

转载 作者:行者123 更新时间:2023-11-29 03:22:18 24 4
gpt4 key购买 nike

我继续收到此错误。现在我已经为我的 SortSearchUtil 获得了它。我尝试进行一些调试,但可以解决问题。错误如下:

 ----jGRASP exec: java PostOffice
Exception in thread "main" java.lang.NullPointerException
at SortSearchUtil.selectionSort(SortSearchUtil.java:106)
at PostOffice.sortLetters(PostOffice.java:73)
at PostOffice.main(PostOffice.java:15)

----jGRASP wedge: exit code for process is 1.
----jGRASP: operation complete.

选择排序的第106行是:

if (array[indexSmallest].compareTo(array[curPos]) > 0)

我不知道我的方法有什么问题。这是我的导师给我的标准方法。我试图调试我的程序,但我很困。这是错误源自的方法,selectionSort:

   public static void selectionSort(Comparable[] array)
{
int curPos, indexSmallest, start;
Comparable temp;
for (start = 0; start < array.length - 1; start++)
{
indexSmallest = start;
for (curPos = start + 1; curPos < array.length; curPos++)
if (array[indexSmallest].compareTo(array[curPos]) > 0)
{
indexSmallest = curPos;
}
// end for
temp = array[start];
array[start] = array[indexSmallest];
array[indexSmallest] = temp;
} // end for
}

sort 方法在底部,它调用了这个 Post Office 方法的 SortSearchUtil.selectionSort:

import java.util.*;
import java.io.*;

public class PostOffice
{

private final int max = 1000;
private Letter [] ltrAra = new Letter[max];
private int count;

public static void main(String [] args)
{
PostOffice postOffice = new PostOffice();
postOffice.readLetters("letters.in");
postOffice.sortLetters();
postOffice.printLetters();
}

public PostOffice()
{
Letter [] Letters = ltrAra;
this.count = 0;
}

public void readLetters(String filename)
{
int count = 0;
int iWork = 0;

Scanner fin = new Scanner(filename);

String toName, toStreet, toCity, toState, toZip;
String fromName, fromStreet, fromCity, fromState, fromZip, temp;
double weight;
String sWork;
fin = FileUtil.openInputFile(filename);
if (fin != null)
{
while (fin.hasNext())
{
toName = fin.nextLine();
toStreet = fin.nextLine();
sWork = fin.nextLine();
iWork = sWork.indexOf(",");
toCity = sWork.substring(0, iWork);
iWork = iWork + 2;
toState = sWork.substring(iWork, iWork + 2);
iWork = iWork + 3;
toZip = sWork.substring(iWork);

fromName = fin.nextLine();
fromStreet = fin.nextLine();
sWork = fin.nextLine();
iWork = sWork.indexOf(",");
fromCity = sWork.substring(0, iWork);
iWork = iWork + 2;
fromState = sWork.substring(iWork, iWork + 2);
iWork = iWork + 3;
fromZip = sWork.substring(iWork);

sWork = fin.nextLine();
weight = Double.parseDouble(sWork);

ltrAra[count] = new Letter(toName, toStreet, toCity, toState, toZip, fromName, fromStreet, fromCity, fromState, fromZip, weight);
count++;
}
fin.close();
}
}

public void sortLetters()
{
SortSearchUtil.selectionSort(ltrAra);
}

public void printLetters()
{
for (Letter ltr : ltrAra)
{
System.out.println(ltr);
System.out.println();
}
}
}

我的文件看起来像这样“letters.in”:

Stu Steiner
123 Slacker Lane
Slackerville, IL 09035
Tom Capaul
999 Computer Nerd Court
Dweebsville, NC 28804-1359
0.50
Tom Capaul
999 Computer Nerd Court
Dweebsville, NC 28804-1359
Chris Peters
123 Some St.
Anytown, CA 92111-0389
1.55

最佳答案

很明显,您获得 NPE 是因为:

您将 ltrAra 初始化为包含 1000 个项目的数组,但您在方法 readLetters() 中读取的项目少于 1000 个。所以在这个数组的末尾,一些空引用保持未初始化(记住数组创建本身不会将单个项目设置为任何对象)。因此,以下排序方法会得到一些空引用 => NPE。

建议的解决方案:

您应该使用 ArrayList 而不是数组,因为这会自动防止您因内部范围检查而访问过多的项目。

关于java - NullPointerException 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22902712/

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