尝试显示特定学生的详细信息时,为什么会显示“ null”?

时间:2019-12-29 11:58:08

标签: java object linked-list

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

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

我已经尝试了所有可以想到的一切,现在由于压力我开始脱发,因此我非常感谢我所能提供的所有帮助。拜托。

下面的所有代码:

下面的

StudentRecords(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;
        }
    }

}

Manager 类别如下:

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();
        }
    }
}

1 个答案:

答案 0 :(得分:1)

如果您要按学生ID选择学生,我会使用

HashMap<Integer, Student>

用于维护您的学生资料库。队列用于FIFO rsp。 LIFO操作。