我收到一个空指针异常,我想不出如何解决。有问题的数组是一个类数组,并且应该可以被使用它的方法访问。
下面是主要方法:
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();
}
这是创建和填充 golfLlist 数组的方法
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(",","")));
}
}
}
这是生成异常的方法(行号是 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()) + " | ");
}
}
与此设置非常相似的东西适用于另一个程序,我很困惑为什么它在这里不起作用。我正在考虑重写它以将数组传递给方法,但这需要大规模重组,我真的不想这样做。
谢谢您的帮助。