我正在写一个程序,该程序创建一个对象,该对象持有来自用户的输入并将其存储在ArraySortedList中。其中一个要求是检查并查看特定对象是否已经在列表中。我遇到的麻烦是每当我输入一组secound信息时,我都会遇到一个错误。参考问题
//Global variables at the top
ListInterface <Golfer> golfers = new ArraySortedList <Golfer> (20);
Golfer golfer;
//Function Variables
Scanner add = new Scanner(System.in);
Golfer tempGolfer;
String name = ".";
int score;
while(!name.equals("")) //Continues until you hit enter
{
System.out.print("\nGolfer's name (press Enter to end): ");
name = add.next();
System.out.print("Golfer's score (press Enter to end): ");
score = add.nextInt();
tempGolfer = new Golfer(name,score);
if(this.golfers.contains(tempGolfer))
System.out.println("The list already contains this golfer");
else
{
this.golfers.add(this.golfer);
System.out.println("\nYou added \'Golfer(" + name + "," + score + ")\' to the list!");
}
}
错误消息:
Exception in thread "main" java.lang.NullPointerException
at ArrayUnsortedList.find(ArrayUnsortedList.java:67)
at ArrayUnsortedList.contains(ArrayUnsortedList.java:110)
at GolfApp.addGolfer(GolfApp.java:90)
at GolfApp.mainMenu(GolfApp.java:52)
at GolfApp.main(GolfApp.java:24)
我几乎可以肯定它的东西做的变量是如何引用,但我真的不知道我怎么能解决这个问题,我有很多的麻烦与变量引用。
如果您想要一组有序的元素,我会使用SortedSet。 –
是的,我宁愿使用不同的ADT,但我们不得不使用这个:P。我必须检查,但谢谢! – adc90