您好,我正在制作一个创建进度报告的多个类的项目。但是,我正在测试方法,但尚未完成该项目,并且遇到了空指针异常。看一下代码,看看是否可以帮助我。请记住,并不是所有方法都只能先着重解决我的问题。我也有一个单独的驱动程序文件,除非另外需要,否则我认为它与发布无关。
学生班:
public class Student {
private String name;
private char grade;
private double average;
private int[] scores = new int[5];
// Constructor
public Student() {
this.name = name;
this.grade = grade;
this.average = average;
this.scores = scores;
}
// Get the Name.
public String getName() {
return name;
}
// Set the Name.
public void setName(String name) {
this.name = name;
}
// Get the Grade.
public char getGrade() {
return grade;
}
// Set the Grade.
public void setGrade(char grade) {
this.grade = grade;
}
// Get the Average.
public double getAverage() {
return average;
}
// Set the Average.
public void setAverage(double average) {
this.average = average;
}
// Get the Scores.
public int[] getScores() {
return scores;
}
// Set the Scores.
public void setScores(int[] scores) {
this.scores = scores;
}
// Determine the average of the five test scores for each student
public void calculateAverage(){
}
public void calculateGrade(){
}
}
ProgressReport类(即时消息获取空指针异常):
public class ProgressReport {
// Create array to hold sections and students.
Student[][] sectionArray = new Student[2][];
// Constructor.
public ProgressReport() {
}
// Get sectionArray.
public Student[][] getSectionArray() {
return sectionArray;
}
// Set sectionArray.
public void setSectionArray(Student[][] sectionArray) {
this.sectionArray = sectionArray;
}
// Read the input file.
public void readInputFile() throws FileNotFoundException{
String line;
int studentNo;
// Open file
File inFile = new File("file.in");
// Create scanner for reading.
Scanner scanner = new Scanner(inFile);
// While inFile has more lines.
while(scanner.hasNext()){
// Read the next line.
line = scanner.nextLine();
// Trim line.
line = line.trim();
//Parse line into int.
studentNo = Integer.parseInt(line);
// For the number of students in section 1 extract data.
for(int i = 0; i<= studentNo; i++){
//Create new student.
sectionArray[0][i] = new Student(); **THIS IS WHERE I GET NULL POINTER EXCEPTION**
// Read next line.
line = scanner.nextLine();
// Create String Tokenizer using a space as the delimiter.
StringTokenizer strTokenizer = new StringTokenizer(line," ");
// While the String Tokeizer has more tokens get data.
while(strTokenizer.hasMoreTokens()){
// Extract name
String name = strTokenizer.nextToken();
// Set name
sectionArray[0][i].setName(name);
int[] scores = new int[5];
// Extract scores.
int score1 = Integer.parseInt(strTokenizer.nextToken());
int score2 = Integer.parseInt(strTokenizer.nextToken());
int score3 = Integer.parseInt(strTokenizer.nextToken());
int score4 = Integer.parseInt(strTokenizer.nextToken());
int score5 = Integer.parseInt(strTokenizer.nextToken());
//Put scores in scores array.
scores[0] = score1;
scores[1] = score2;
scores[2] = score3;
scores[3] = score4;
scores[4] = score5;
// Set scores.
sectionArray[0][i].setScores(scores);
}
}
}
}
// Generate a report.
public void generateReport(){
System.out.println("Progress Report\n");
System.out.println("Section 1");
System.out.println(sectionArray[0][0].getName());
}
// Sort by name.
public void sortByName(){
}
// Binary search.
public Student binarySearch(int section, String searchName){
return null;
}
}
我并没有要求任何人完成我的工作,只是请解释为什么我会得到一个空指针异常。
最佳答案
一旦知道Student
的数量为
studentNo = Integer.parseInt(line);
// initialize the Array
sectionArray[0] = new Student[studentNo];
// For the number of students in section 1 extract data.
for(int i = 0; i<= studentNo; i++){
您一直将
sectionArray
用作sectionArray[0][*]
。我不确定您是否真的需要二维数组。将其初始化为new Student[2][];
表示您有时也会使用sectionArray[1][*]
。如果以后再做;您还需要初始化
sectionArray[1]
。