Hello I am making a project of multiple classes that creates a progress report. However I am testing out methods and am not yet complete with the project and came across a null pointer exception. Take a look at the code and see if you can help me out please. Keep in mind all methods aren't finished just trying to focus on my problem first. I also have a separate driver file that i do not find relevant to post, unless needed otherwise.
Student class:
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 class (where im getting the null pointer exception):
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;
}
}
I'm not asking anyone to finish my work, just explain why I am getting a null pointer exception please.