0

I am getting a null pointer exception that I cannot think how to fix. The array in question is a class array, and should be accessible to the method that is utilizing it.

Here is the main method:

static Golfer[] golfList;
static Course currentCourse;

public static void main(String[] args) {
    System.out.println("Testing data integrity.  Printing files:");
    String golferData = readFile("scores.txt");
    String courseData = readFile("course.txt");
    parseGolferData(golferData);
    parseCourseData(courseData);
    printResults();
}

Here is the method that creates and populates the golfLlist array

public static void parseGolferData(String golfFileData){
    String[] firstSplit = golfFileData.split("\\\n");
    String[] secondSplit = firstSplit[1].split(", ");
    Golfer[] golfList = new Golfer[secondSplit.length];
    for (int i = 0; i < secondSplit.length; i++){
        System.out.println(secondSplit[i]);
        golfList[i] = new Golfer(secondSplit[i], 18);
        golfList[i].getName();
    }

    for (int i = 2; i<firstSplit.length; i++){
        String[] split = firstSplit[i].split(", ");
        for (int j = 0; j<split.length-1; j++){
            golfList[j].setScore(i-1, Integer.parseInt(split[j].replace(",","")));
        }
    }
}

Here is the method that is generating the exception (line number is the first instance of golfList[i]

public static void printResults(){
    System.out.print("| Par | ");

    for (int i = 0; i<golfList.length; i++){
        System.out.print(golfList[i].getName() + " | ");
    }
    System.out.println(); // spacing
    for (int i = 0; i<18; i++){
        System.out.print("| " + currentCourse.getPar(i+1) + " | ");
        for (int j = 0; j < golfList.length; j++){
            System.out.print(golfList[i].getScore(i+1) + " | ");
        }
    }

    System.out.println(); // spacing
    System.out.print("| " + currentCourse.totalPar() + " | ");
    for (int i = 0; i < golfList.length; i++){
        System.out.print(golfList[i].getTotal() + " | ");
    }
    System.out.println(); // spacing
    System.out.print("|   | ");
    for (int i = 0; i < golfList.length; i++){
        System.out.print((golfList[i].getTotal() - currentCourse.totalPar()) + " | ");
    }
}

Something very similar to this setup worked fine for another program, and I'm confused as to why it will not work here. I'm considering rewriting it to pass the array into the methods, but this would require massive restructuring and I don't really want to have to do that.

Thank you for your help.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Marshall Tigerus
  • 3,675
  • 10
  • 37
  • 67

4 Answers4

3

The array in question is a class array,

These arrays are all local arrays:

String[] firstSplit = golfFileData.split("\\\n");
String[] secondSplit = firstSplit[1].split(", ");
Golfer[] golfList = new Golfer[secondSplit.length];

If you defined these arrays as class arrays then your code should be:

firstSplit = golfFileData.split("\\\n");
secondSplit = firstSplit[1].split(", ");
golfList = new Golfer[secondSplit.length];

Also I see too many static methods. Only the main() method should be static. All the other methods should be part of your class if you want to use class variables.

camickr
  • 321,443
  • 19
  • 166
  • 288
2

You are shadowing your class array by redeclaring it inside parseGolferData(). The array your initialize is local to function, and not class array. So when you access it inside printResults() you get a NullPointerException.

Change this ...

Golfer[] golfList = new Golfer[secondSplit.length];

to

golfList = new Golfer[secondSplit.length];
vidit
  • 6,293
  • 3
  • 32
  • 50
1

There is limited information, but...

golfList is defined locally within parseGolferData

Golfer[] golfList = new Golfer[secondSplit.length];

Meaning that once the method exists, the reference is lost. This would suggest that golfList has already been declared else where, other wise the statement in printResults would cause a compiler error.

It would appear that you are shadowing your variables. Try changing the initialisation of golfList in parseGolferData to look more like...

golfList = new Golfer[secondSplit.length];

Instead. In fact, all the arrays initialised within this method probably suffer from the same problem.

You could solve this problem, by, as you say, passing the array references as parameters to the other methods...

vidit
  • 6,293
  • 3
  • 32
  • 50
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
1

In addition to what others have said, whenever you're doing anything like:

 for (int i = 0; i<golfList.length; i++)
    {
        System.out.print(golfList[i].getName() + " | ");
    }

You should be doing:

 for (int i = 0; i<golfList.length; i++)
    {   
        if(golfList[i] != null)
        System.out.print(golfList[i].getName() + " | ");
    }
Steve P.
  • 14,489
  • 8
  • 42
  • 72