-1

我用 Java Swing 编写了一个库登录页面并尝试运行它。该页面可以正常运行,但是当我输入任何用户名时,选择一个类型并按登录,它会引发一个AWT-EventQueue-0: NullPointerException.

Library 类反序列化包含用户和书籍信息的两个文件,并将它们初始化为对象。

User 是 Member 和 Staff 的父类,Book 类代表一本书及其标题、描述、副本。这些类的方法都是正确的。

登录窗口.java

import java.awt.EventQueue;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;

public class LoginWindow {

    private JFrame frmLogIn;
    private JTextField textField;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    LoginWindow window = new LoginWindow();
                    window.frmLogIn.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Initialize the contents of the frame.
     */
    public LoginWindow() {

        frmLogIn = new JFrame();

        JLabel lblName = new JLabel("Name:");
        lblName.setBounds(23, 25, 46, 14);
        frmLogIn.getContentPane().add(lblName);

        JLabel lblType = new JLabel("Type:");
        lblType.setBounds(23, 56, 46, 14);
        frmLogIn.getContentPane().add(lblType);

        JTextField textField = new JTextField();
        textField.setBounds(71, 22, 158, 20);
        frmLogIn.getContentPane().add(textField);
        textField.setColumns(10);

        JComboBox comboBox = new JComboBox();
        comboBox.setModel(new DefaultComboBoxModel(new String[] {"Staff", "Member"}));
        comboBox.setBounds(71, 53, 158, 20);
        frmLogIn.getContentPane().add(comboBox);

        JButton btnNewButton = new JButton("Login");
        btnNewButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent arg0) {

                if (!Library.users.containsKey(textField.getText())) {
                    new CreateUserWindow();
                    frmLogIn.dispose();
                }

                else if (!Library.users.get(textField.getText()).getUserType().equals(comboBox.getActionCommand())) {
                    JOptionPane.showMessageDialog(null,
                            "The user name and user type do not match, please try again.",
                            "User information mismatch", JOptionPane.ERROR_MESSAGE);
                }

                else {
                    String type = comboBox.getActionCommand();
                    if (type.equals("Staff")) new StaffWindow((Staff)Library.users.get(textField.getText()));
                    else new MemberWindow((Member)Library.users.get(textField.getText()));
                }
            }
        });
        btnNewButton.setBounds(23, 88, 206, 23);
        frmLogIn.getContentPane().add(btnNewButton);


        frmLogIn.setTitle("Log in");
        frmLogIn.setBounds(100, 100, 268, 185);
        frmLogIn.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frmLogIn.getContentPane().setLayout(null);
        frmLogIn.setVisible(true);
    }
}

图书馆.java

import java.io.*;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class Library implements Serializable{

    public static HashMap<String, User> users;
    public static HashMap<String, Book> books;

    public Library(boolean readFromSerialized) throws IOException, ClassNotFoundException {
        users = new HashMap<>();
        books = new HashMap<>();

        if (readFromSerialized) {
            ObjectInputStream u_in = new ObjectInputStream(new FileInputStream("./Assignment/data/Users.txt"));
            ObjectInputStream b_in = new ObjectInputStream(new FileInputStream("./Assignment/data/Books.txt"));

            users = (HashMap<String, User>) u_in.readObject();
            books = (HashMap<String, Book>) b_in.readObject();

            u_in.close();
            b_in.close();
        }
    }

    public static void serializeToFile() {
        try {
            ObjectOutputStream u_out = new ObjectOutputStream(new FileOutputStream("./Assignment/data/Users.txt"));
            ObjectOutputStream b_out = new ObjectOutputStream(new FileOutputStream("./Assignment/data/Books.txt"));

            u_out.writeObject(Library.users);
            b_out.writeObject(Library.books);

            u_out.close();
            b_out.close();
        } catch (Exception ex) {}
    }
}

错误消息显示以下代码段包含一些错误,该代码是关于用户信息验证的:

if (!Library.users.containsKey(textField.getText())) {
                    new CreateUserWindow();
                    frmLogIn.dispose();
                }
4

1 回答 1

1

您的代码中有几个问题:

  1. ( )的使用setBounds和使用,这是邪恶的并且不受欢迎,因为它可能会导致诸如此类的烦人问题请使用适当的布局管理器以使您的应用程序在所有操作系统和 PLAF 中正确呈现。null-layoutsetLayout(null)

  2. 使用public static成员,这可能会导致您的应用程序流程不一致。

  3. 现在,您的问题是您创建了一个全局 textfield变量但从不初始化它(所以它是null

    private JTextField textField;
    

    但是,在您的构造函数中,您创建另一个具有相同名称的构造函数,但它是一个局部变量:

    JTextField textField = new JTextField();
    

    当您调用此行时:

    else if (!Library.users.get(textField.getText()).getUserType().equals(comboBox.getActionCommand())) {
    

    Java 使用的是全局变量(记住它null),而不是局部变量(已初始化),为了解决这个问题,删除局部变量JTextField的声明:

    JTextField textField = new JTextField();
    

    所以,它变成:

    textField = new JTextField();
    

    这将初始化全局变量,您将能够在您的ActionListener

于 2019-06-07T19:49:36.360 回答