Reputation: 303
I'm writing a program that creates an object which holds to inputs from the user and stores it in an ArraySortedList. One of the requirements is to check and see if the particular object is already in the list. The trouble I'm having is whenever I input a secound set of information I get an error.
//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!");
}
}
Error Message:
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)
I'm almost sure it's something to do with how the variable is referenced but I'm not really sure how I could fix it, I have a lot of trouble with variable referencing.
Upvotes: 1
Views: 86
Reputation: 4993
Your golfer variable is not initalized. try with :
this.golfers.add(tempGolfer);
regards.
Upvotes: 3