您好,我正在制作一个创建进度报告的多个类的项目。但是,我正在测试方法,但尚未完成该项目,并遇到了空指针异常。看看代码,看看你是否可以帮助我。请记住,所有方法都没有完成,只是首先尝试关注我的问题。我还有一个单独的驱动程序文件,我发现它与发布无关,除非另有需要。
学生班级:
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;
}
}
我不是要求任何人完成我的工作,只是解释一下为什么我得到一个空指针异常。