我正在寫一個程序,該程序創建一個對象,該對象持有來自用戶的輸入並將其存儲在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