我編寫了一個名爲TestStudent的程序,它創建了Student對象的四個元素數組。提示用戶輸入學號,姓名,部門和分類等級。下面的代碼:四元素數組對象setter方法?
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());
線。有關如何解決這個問題的任何建議?
['this'](http://stackoverflow.com/questions/5364278/creating-an-array-of-objects-in-java)是你的問題 – GoldRoger