Thank you for taking the time to help me out. I've got 3 classes which are interacting with eachother: Score, Golfer, and GolferTester. Golfer contains an array of Score objects. My issue is with adding singular Score objects to the array via Golfer.addScore(String, int, double, int, String) when running GolferTester. I have tried to rearrange Golfer.addScore() many times now and am still receiving NullPointerException when executing Golfer.findScore(String).
I think the most important aspect here is that everything runs smoothly so long as I refrain from prematurely ending the Golfer.addScore() for loop with return. But not stopping the loop fills the entire array with nextScore, and renders my method for finding which array slot to fill (by locating null slots), useless after one call. I'll show the code below. Thank you for your assistance!
Golfer.addscore(String newCourse, int newScore, double newCourseRating, int newSlope, String newDate) :
public void addScore(String newCourse, int newScore,
double newCourseRating, int newSlope, String newDate)
{
Score nextScore = new Score(newCourse, newDate, newScore,
newCourseRating, newSlope);
for (int i = 0; i < scores.length; i++)
{
if (scores[i] == null)
{
scores[i] = nextScore;
return;
}
}
}
Golfer.findScore(String date) :
private int findScore(String date)
{
int ans = 0;
for (int i = 0; i < scores.length; i++)
{
String iDate = scores[i].getDate();
if (iDate.equals(date))
ans = i;
}
if (ans == 0)
return -1;
else
return ans;
}
Golfer.getScore(String date) :
public Score getScore(String date)
{
int a = findScore(date);
return scores[a];
}
class GolferTester :
public class GolferTester
{
public static void main (String[] args)
{
Golfer golfer1 = new Golfer("John", "homecourse", 4);
golfer1.addScore("course1", 75, 68.5, 105, "05/03/2017");
System.out.println(golfer1.getScore("05/03/2017"));
}
}
As mentioned, simply removing the return aspect from the for loop within Golfer.addScore() makes everything run properly. I've tried workarounds that don't involve using "return" specifically, but to no avail. Thanks again for any input.