I have a 2D Array called sectionArray which is of type Student class. Each student has a name and an array[] of 5 grades for exam scores. These names and scores are divided into to sections in a file that must be read. I keep getting a nullPointer on my sectionArray no matter what I change. Thank you for any suggestions.
我有一个名为sectionArray的2D数组,它是Student类的类型。每个学生都有一个名称和一个5级的数组[]为考试成绩。这些名称和分数分为必须读取的文件中的部分。无论我改变什么,我都会在我的sectionArray上得到一个nullPointer。谢谢你的任何建议。
import java.util.*;
import java.io.*;
public class ProgressReport {
private Student[][] sectionArray;// each student has a name,
// grade, average,
// and the array of scores
private File file;
private Scanner inputFile;
public ProgressReport() throws IOException {
sectionArray = new Student[2][];
file = new File("Lab5A.in.txt");
inputFile = new Scanner(file);
}
/**
*
* @return the values in the sectionArray
*/
public Student[][] getSectionArray() {
return sectionArray;
}
/**
* initialize sectionArray and set the values
*
* @param sectionArray
* passed 2D array of Students
*/
public void setSectionArray(Student[][] sectionArray) {
for (int i = 0; i < sectionArray.length; i++) {
for (int j = 0; j < sectionArray[i].length; j++) {
this.sectionArray[i][j] = sectionArray[i][j];
}
}
}
/**
* reads from the file and creates new Students
*
* @throws IOException
*/
public void readInputFile() throws IOException {
int colNum = 0;
int section = 0;
String name = " ";
int[] grades = new int[5];
while (inputFile.hasNext()) {
colNum = inputFile.nextInt();// gets size of row
sectionArray[section] = new Student[colNum];// initialize array
// iterates through colNum amount of times
for (int j = 0; j < colNum; j++) {
name = inputFile.next();// gets next name in column
for (int i = 0; i < 5; i++) {
// stores scores for that name
grades[i] = inputFile.nextInt();
}
// creates new Student with name and grades
sectionArray[section][j] = new Student(name, grades);
section++;
}
}
// passes the values in sectionArray
setSectionArray(sectionArray);
}
}
My student class looks like this:
我的学生班看起来像这样:
public class Student {
private String name = " "; // Store the name of the student
private char grade; // Store the letter grade
private double average; // Store the average score
private int[] scores; // Store the five exam scores
public Student() {
grade = ' ';
average = 0.0;
}
public Student(String name, int[] score) {
this.name = name;
scores = new int[5];
for (int i = 0; i < scores.length; i++) {
scores[i] = 0;
}
for (int i = 0; i < scores.length; i++) {
this.scores[i] = score[i];
}
}
// getters
public String getName() {
return name;
}
public char getGrade() {
return grade;
}
public double getAverage() {
return average;
}
// think about changing this to return a different format
public int[] getScores() {
return scores;
}
// setters
public void setScore(int[] scores) {
}
public void setName(String name) {
this.name = name;
}
public void setGrade(char grade) {
this.grade = grade;
}
public void setAverage(double average) {
this.average = average;
}
/**
* determine the average of the five test scores for each student
*/
public void calculateAverage() {
double total = 0;
double average = 0;
for (int i = 0; i < scores.length; i++) {
total += scores[i];
}
average = total / scores.length;
setAverage(average);
}
/**
* Determine the student's letter grade based on average of test scores
*/
public void calculateGrade() {
double average = 0;
average = getAverage();
if (average <= 100 && average >= 90) {
setGrade('A');
} else if (average <= 89 && average >= 80) {
setGrade('B');
} else if (average <= 79 && average >= 70) {
setGrade('C');
} else if (average <= 69 && average >= 60) {
setGrade('D');
} else if (average <= 59 && average >= 0) {
setGrade('F');
}
}
public String toString() {
return getName() + " " + getAverage() + " " + getGrade();
}
}
1 个解决方案
#1
2
The array you are trying to write is initialized with sectionArray = new Student[2][];
. This will create a matrix (2D array) with 2 columns and only one row, and then you try to set your new values on this array. If you already know the size of the matrix you are going to read from the file, then initialize it with the correct values.
您尝试编写的数组使用sectionArray = new Student [2] [];进行初始化。这将创建一个包含2列且只有一行的矩阵(2D数组),然后尝试在此数组上设置新值。如果您已经知道要从文件中读取的矩阵的大小,则使用正确的值初始化它。
Anyway, I did not get why you are trying to use a 2D array for this. If I understood correctly the purpose of your code, you should be using a List
instead to store the read data, and since it has a dynamically increasing size you wouldn't have to bother with controlling the indexes like you do with the array. Take a look at this tutorial to learn how to use lists.
无论如何,我不明白你为什么要尝试使用2D数组。如果我正确理解了代码的用途,那么您应该使用List来存储读取数据,并且由于它具有动态增加的大小,因此您不必像控制数组一样来控制索引。请查看本教程以了解如何使用列表。
#1
2
The array you are trying to write is initialized with sectionArray = new Student[2][];
. This will create a matrix (2D array) with 2 columns and only one row, and then you try to set your new values on this array. If you already know the size of the matrix you are going to read from the file, then initialize it with the correct values.
您尝试编写的数组使用sectionArray = new Student [2] [];进行初始化。这将创建一个包含2列且只有一行的矩阵(2D数组),然后尝试在此数组上设置新值。如果您已经知道要从文件中读取的矩阵的大小,则使用正确的值初始化它。
Anyway, I did not get why you are trying to use a 2D array for this. If I understood correctly the purpose of your code, you should be using a List
instead to store the read data, and since it has a dynamically increasing size you wouldn't have to bother with controlling the indexes like you do with the array. Take a look at this tutorial to learn how to use lists.
无论如何,我不明白你为什么要尝试使用2D数组。如果我正确理解了代码的用途,那么您应该使用List来存储读取数据,并且由于它具有动态增加的大小,因此您不必像控制数组一样来控制索引。请查看本教程以了解如何使用列表。