-4

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.

  • 1
    Exactly which line gives you the NullPointerException? –  May 03 '17 at 16:31
  • 1
    What's the exact error? – Carcigenicate May 03 '17 at 16:31
  • Exception in thread "main" java.lang.NullPointerException – Uncalledfor May 03 '17 at 16:33
  • In `findScore` you probably have null values in the array, and you try to check the date of a `null` value in the line `String iDate = scores[i].getDate();`. Add the stack trace for more details – Sigrist May 03 '17 at 16:34
  • and proceeds to highlight "String iDate = scores[i].getDate();" within Golfer.findScore() – Uncalledfor May 03 '17 at 16:34
  • Get a java chat for this . Unrelated int ans = 0; make this =-1 and later just return ans. What if the first iteration for (int i = 0; i < scores.length; i++) if (iDate == date) is right? Also if you have a date why not used java.util.Date or new date api? – tgkprog May 03 '17 at 16:35
  • 2
    looks like scores is never initialized, which later will explode since you are doing iDate == date with strings in the findScore method.... – ΦXocę 웃 Пepeúpa ツ May 03 '17 at 16:35
  • I've purposely filled the array with null values in order to reference the empty slots, but the issue is that Golfer.addScore() is not replacing what would be `scores[0] == null` on the first round of `for (int i = 0; i < scores.length; i++)`. – Uncalledfor May 03 '17 at 16:44
  • For intents and purposes, the date variable is sufficient as type String for now. – Uncalledfor May 03 '17 at 16:46
  • I'm aware of what a NullPointerException is, why downvote this based on assumptions? – Uncalledfor May 03 '17 at 17:00
  • @Uncalledfor I think the downvotes are because your question is very hard to read, and your actual problem isn't clear. I've read the question through 4 times, and it seems like you're asking how to solve a NPE, then you say you know what it is and how to solve it. What is your question? – Carcigenicate May 03 '17 at 19:04
  • @ Carcigenicate I'll just post all of the code next time instead of just the problem areas. It seems to me that this is what most of you guys look at right away. It was labelled a duplicate right away since OXoce(whatevertheF) assumed I hadn't initialized scores[], simply because that part of the code was not provided in my query. – Uncalledfor May 03 '17 at 20:24
  • I found the problem to be in the method `findScore(String)` where the first loop executes properly, but continued on to attempt to reference the next Score in scores[], at which point it generated the NullPointerException. So the issue, as i said, was not with the exception itself (maybe I shouldn't have put it in the title), but was with ending the for loop inside `findScore(String)` before it referenced the next (null) object in the scores[] array. TL;DR logic error of my own oversight. (for loop within Golfer.findScore(String)) – Uncalledfor May 03 '17 at 20:31

1 Answers1

0

In findScore you call String iDate = scores[i].getDate();. What happens if the date is not found before the iteration finds an empty (null) value? This is your problem.

Also, later on, you use == to compare strings. This will not work. You want to use String.equals(otherString)

Stephen
  • 4,041
  • 22
  • 39
  • Right, I'm aware that if `String iDate = scores[i].getDate();` returns a null value then the error occurs, but I'm under the impression that it should not be returning null. I was hoping that fixing the `==` issue would simply resolve it, but it has unfortunately not. – Uncalledfor May 03 '17 at 16:55