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.