为什么我在尝试显示特定学生的详细信息时得到"null"?



将学生对象添加到链表后,我尝试显示该学生的详细信息,但总是在输出中以null结束。

StudentRecords (GUI(类中的addStudent()方法旨在调用Manager类中的方法:addStudent(Student aNewStudentObj),然后使用用户输入的详细信息创建学生的新对象,并将该对象存储到链表中。然后,Manager 类中的displayStudent(int studentId)方法应该能够显示给定studentId的学生及其详细信息,但就我而言,我只得到了已经提到的null

我真的尝试了我能想到的一切,现在由于压力,我开始失去头发,所以我非常感谢我能得到的所有帮助。请。

以下所有代码:

学生记录 (GUI(课程如下:

package studentrecords;
/**
*
* @author azim1
*/
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
import java.util.Queue;
import javax.swing.*;
public class StudentRecords implements ActionListener {
private JTextField firstNameTextField;
private JTextField surnameTextField;
private JTextField yearOfStudyTextField;
private JTextField emailTextField;
private JTextField studentIdTextField;
private JTextField moduleCodeTextField;
private JTextField moduleMarkTextField;
private JButton addStudentButton;
private JButton addModuleButton;
private JButton displayStudentButton;
private JButton displayAllButton;
private JButton displayMarksButton;
private JButton deleteStudentButton;
private JFrame frame;
private Manager studentList = new Manager();
private Student student;
public StudentRecords() {
frame = new JFrame("Student Records");
Container contentPane = frame.getContentPane();
contentPane.setLayout(new GridLayout(9, 3));
JLabel firstNameLabel = new JLabel("First Name:");
contentPane.add(firstNameLabel);
JLabel surnameLabel = new JLabel("Surname:");
contentPane.add(surnameLabel);
JLabel nothing = new JLabel(" ");
contentPane.add(nothing);
firstNameTextField = new JTextField(15);
contentPane.add(firstNameTextField);
surnameTextField = new JTextField(15);
contentPane.add(surnameTextField);
addStudentButton = new JButton("Add Student");
contentPane.add(addStudentButton);
addStudentButton.addActionListener(this);
JLabel yearOfStudyLabel = new JLabel("Year of Study:");
contentPane.add(yearOfStudyLabel);
JLabel emailLabel = new JLabel("Email:");
contentPane.add(emailLabel);
JLabel nothing1 = new JLabel(" ");
contentPane.add(nothing1);
yearOfStudyTextField = new JTextField(15);
contentPane.add(yearOfStudyTextField);
emailTextField = new JTextField(15);
contentPane.add(emailTextField);
JLabel nothing2 = new JLabel(" ");
contentPane.add(nothing2);
JLabel studentIdLabel = new JLabel("Student ID:");
contentPane.add(studentIdLabel);
JLabel nothing3 = new JLabel(" ");
contentPane.add(nothing3);
JLabel nothing4 = new JLabel(" ");
contentPane.add(nothing4);
studentIdTextField = new JTextField(15);
contentPane.add(studentIdTextField);
displayStudentButton = new JButton("Display Student");
contentPane.add(displayStudentButton);
displayStudentButton.addActionListener(this);
displayMarksButton = new JButton("Display Marks");
contentPane.add(displayMarksButton);
displayMarksButton.addActionListener(this);
JLabel moduleCodeLabel = new JLabel("Module Code:");
contentPane.add(moduleCodeLabel);
JLabel moduleMarkLabel = new JLabel("Module Mark:");
contentPane.add(moduleMarkLabel);
JLabel nothing5 = new JLabel(" ");
contentPane.add(nothing5);
moduleCodeTextField = new JTextField(15);
contentPane.add(moduleCodeTextField);
moduleMarkTextField = new JTextField(15);
contentPane.add(moduleMarkTextField);
addModuleButton = new JButton("Add Module");
contentPane.add(addModuleButton);
addModuleButton.addActionListener(this);
displayAllButton = new JButton("Display All");
contentPane.add(displayAllButton);
displayAllButton.addActionListener(this);
JLabel nothing6 = new JLabel(" ");
contentPane.add(nothing6);
deleteStudentButton = new JButton("Delete Student");
contentPane.add(deleteStudentButton);
deleteStudentButton.addActionListener(this);
frame.pack();
frame.setVisible(true);
}
public static void main(String[] args) {
StudentRecords sRecord = new StudentRecords();
}
public void actionPerformed(ActionEvent event) {
String command = event.getActionCommand();
if (command.equals("Add Student")) {
addStudent();
}
if (command.equals("Add Module")) {
addModule();
}
if (command.equals("Display All")) {
displayAll();
}
if (command.equals("Display Student")) {
displayStudent();
}
if (command.equals("Display Marks")) {
displayMarks();
}
if (command.equals("Delete Student")) {
deleteStudent();
}
}
public String getFirstName() {
String firstName
= firstNameTextField.getText();
return firstName;
}
public String getSurname() {
String surname
= surnameTextField.getText();
return surname;
}
public int getYearOfStudy() {
int yearOfStudy
= Integer.parseInt(yearOfStudyTextField.getText());
return yearOfStudy;
}
public String getEmail() {
String email
= emailTextField.getText();
return email;
}
public int getStudentId() {
int studentId
= Integer.parseInt(studentIdTextField.getText());
return studentId;
}
public String getModuleCode() {
String moduleCode
= moduleCodeTextField.getText();
return moduleCode;
}
public int getModuleMark() {
int moduleMark
= Integer.parseInt(moduleMarkTextField.getText());
return moduleMark;
}
public void addStudent() {
studentList.addStudent(student);
}
public void addModule() {
//search for the student withh the ID
Student module = new Student(getFirstName(), getSurname(), getEmail(), getYearOfStudy(), getStudentId());
module.addModule(getStudentId(), getModuleCode(), getModuleMark());
}
public void displayAll() {
studentList.displayAll();
}
public void displayStudent() {
studentList.displayStudent(getStudentId());
}
public void displayMarks() {
studentList.displayMarks(getStudentId());
}
public void deleteStudent() {
studentList.deleteStudent(getStudentId());
}
}

学生班级如下:

import java.util.Arrays;
import java.util.Collections;
import java.util.LinkedList;
import java.util.Queue;
/**
*
* @author azim1
*/
public class Student {
private String firstName;
private String surname;
private String email;
private int yearOfStudy;
private int studentId;
private Queue<Module> mods = new LinkedList<>();
public Student(String firstName, String surname, String email, int yearOfStudy, int studentId) {
this.firstName = firstName;
this.surname = surname;;
this.email = email;
this.yearOfStudy = yearOfStudy;;
this.studentId = studentId;
}
public String getFirstName() {
return firstName;
}
public String getSurname() {
return surname;
}
public String getEmail() {
return email;
}
public int getYearOfStudy() {
return yearOfStudy;
}
public int getStudentId() {
return studentId;
}
public String print() {
return "Student ID: " + studentId + "n"
+ "First Name: " + firstName + "n"
+ "Surname: " + surname + "n"
+ "Email: " + email + "n"
+ "Year of Study: " + yearOfStudy;
}
public void addModule(int id, String mCode, int mMark) {
if (mods.size() == 4) {
mods.remove();
}
Module module = new Module();
mods.add(module);
}
//this method returns the module list of this student sorted by marks
public String getModulesSortedByMarks(int id) {
Object[] sortedMods;
sortedMods = mods.toArray();
Arrays.sort(sortedMods);
String sortedModulesList = "";
for (int i = 0; i < sortedMods.length; i++) {
sortedModulesList = "n" + ((Module) sortedMods[i]).print();
}
return sortedModulesList;
}
}

模块类如下:

package studentrecords;
/**
*
* @author azim1
*/
public class Module implements Comparable {
private String moduleCode;
private int moduleMark;
public String getModuleCode() {
return moduleCode;
}
public int getModuleMark() {
return moduleMark;
}
public String print() {
return "Module Code: " + moduleCode + "n"
+ "Mark: " + moduleMark;
}
@Override
public int compareTo(Object aModule) {
if (this.moduleMark == ((Module) aModule).getModuleMark()) {
return 0;
} else if (this.moduleMark < ((Module) aModule).getModuleMark()) {
return -1;
} else {
return 1;
}
}
}

经理类如下:

package studentrecords;
import java.util.LinkedList;
import java.util.List;
/**
*
* @author azim1
*/
public class Manager {
List<Student> studs = new LinkedList<>();
Student student;
public void addStudent(Student aNewStudentObj) {
studs.add(aNewStudentObj);
}
public void displayStudent(int studentId) {
System.out.println(studs.get(studentId));
}
public void displayMarks(int studentId) {
student.getModulesSortedByMarks(studentId);
}
public void deleteStudent(int studentId) {
studs.remove(studentId);
}
public void displayAll() {
for (Student student : studs) {
student.print();
System.out.println();
}
}
}

如果你想通过它的 studentId 选择你的学生,我会使用

HashMap<Integer, Student>

用于维护您的学生仓库。队列用于 FIFO RSP。后进先出操作。

相关内容

  • 没有找到相关文章

最新更新