我编写了一个名为TestStudent的程序,它创建了Student对象的四个元素数组。用户提示输入学生ID,姓名,部门和分类级别。这是代码:
import java.util.Scanner;
public class TestStudent {
public static void main(String[] args) {
//Create a Scanner object
Scanner input = new Scanner(System.in);
//Create a four element array of Student object
Student[] studentList = new Student[4];
for (int i = 0; i < studentList.length; i++) {
int j = i + 1;
//Prompt user to enter student matrix number
System.out.println("Enter student " + j + " id : ");
studentList[i].setIdStudent(input.nextInt());
//Prompt user to enter student name
System.out.println("Enter student " + j + " name : ");
studentList[i].setName(input.nextLine());
//Prompt user to enter student department
System.out.println("Enter student " + j + " department : ");
studentList[i].setDepartment(input.nextLine());
//Prompt user to enter student classification level
System.out.println("Enter student " + j + " classification : ");
studentList[i].setClassification(input.next());
System.out.println("\n");
}
//Print result
System.out.println("Id Student Name Department Classification");
System.out.println("******************************************************************************");
for (int i = 0; i < studentList.length; i++) {
System.out.println(studentList[i].getIdStudent() + " " + studentList[i].getName() + " " + studentList[i].getDepartment() + " " + studentList[i].getClassification());
}
}
public class Student {
//Student matrix number
private int idStudent;
//Student name
private String name;
//Student department
private String department;
//Student classification level
private String classification;
//Construct a default Student object
public Student() {
idStudent = 0;
name = " ";
department = " ";
classification = " ";
}
//Construct a Student object
public Student(int idStudent, String name, String department, String classification) {
this.idStudent = idStudent;
this.name = name;
this.department = department;
this.classification = classification;
}
//Return student matrix number
public int getIdStudent() {
return idStudent;
}
//Return student name
public String getName() {
return name;
}
//Return student department
public String getDepartment() {
return department;
}
//Return student classification level
public String getClassification() {
return classification;
}
//Set student matrix number
public void setIdStudent(int newIdStudent) {
newIdStudent = idStudent;
}
//Set student name
public void setName(String newName) {
newName = name;
}
//Set student department
public void setDepartment(String newDepartment) {
newDepartment = department;
}
//Set student classification level
public void setClassification(String newClassification) {
newClassification = classification;
}
}
}
预期输出为:
Id Student Name Department Classification
********************************************************************************
1140 Will Gerard Music Junior
1152 Julia Ross Architecture Freshman
1130 Fred Huan Medic Senior
1137 Clara Whist Aviation Sophomore
但是发生了错误:
Enter student 1 id :
1140
Exception in thread "main" java.lang.NullPointerException
at Asg2.TestStudent.main(TestStudent.java:28)
我认为问题似乎是studentList[i].setIdStudent(input.nextInt());
行。有关如何解决这个问题的任何建议?
答案 0 :(得分:1)
1)转到第28行。
2)你有一些具有空值的参考文献 在您尝试在其上调用方法或访问时 其中一个字段(类变量)。
3)确保在尝试之前初始化该参考 使用它,所以当你需要它时它不是空的 非空值。