尝试显示学生的详细信息时为什么会出现NullPointerException?

时间:2019-12-30 11:31:09

标签: java

在将学生对象添加到“链接列表”之后,我尝试显示该学生的详细信息,但始终在输出端出现NullPointerException。

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

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

下面的所有代码:

下面的

StudentRecords(GUI)类:

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 static Student student;

    public StudentRecords(Student student) {

        this.student = student;

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

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

    public void displayMarks() {

        studentList.displayMarks(student);
    }

    public void deleteStudent() {

        studentList.deleteStudent(student);
    }

}

学生课程如下:

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(Student 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 类别如下:

import java.util.Iterator;
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(Student studentId) {

        Iterator<Student> it = studs.iterator();
        while (it.hasNext()) {
            Student s = it.next();
            if (s.equals(studentId)) {
                System.out.println(s);
            }
        }
    }

    public void displayMarks(Student studentId) {

        student.getModulesSortedByMarks(studentId);
    }

    public void deleteStudent(Student studentId) {

        Iterator<Student> it = studs.iterator();
        while (it.hasNext()) {
            if (it.next().equals(studentId)) {
                it.remove();
            }
        }
    }

    public void displayAll() {

        Iterator<Student> it = studs.iterator();
        while (it.hasNext()) {
            System.out.println(it.next());
        }
    }
}

错误跟踪:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at studentrecords.Manager.displayStudent(Manager.java:31)
    at studentrecords.StudentRecords.displayStudent(StudentRecords.java:225)
    at studentrecords.StudentRecords.actionPerformed(StudentRecords.java:155)
    at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
    at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2348)
    at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
    at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
    at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
    at java.awt.Component.processMouseEvent(Component.java:6539)
    at javax.swing.JComponent.processMouseEvent(JComponent.java:3324)
    at java.awt.Component.processEvent(Component.java:6304)
    at java.awt.Container.processEvent(Container.java:2239)
    at java.awt.Component.dispatchEventImpl(Component.java:4889)
    at java.awt.Container.dispatchEventImpl(Container.java:2297)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4904)
    at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4535)
    at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4476)
    at java.awt.Container.dispatchEventImpl(Container.java:2283)
    at java.awt.Window.dispatchEventImpl(Window.java:2746)
    at java.awt.Component.dispatchEvent(Component.java:4711)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:760)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:84)
    at java.awt.EventQueue$4.run(EventQueue.java:733)
    at java.awt.EventQueue$4.run(EventQueue.java:731)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:74)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:730)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:205)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

0 个答案:

没有答案