我用Java Swing编写了一个图书馆登录页面,然后尝试运行它。该页面可以正常运行,但是当我输入任何用户名时,请选择一种类型并按登录,它将引发一个AWT-EventQueue-0: NullPointerException
。
Library类反序列化包含用户和书籍信息的两个文件,并将它们初始化为对象。
User是Member and Staff的父类,Book类代表一本书,其标题,说明和副本。这些类的方法都是正确的。
LoginWindow.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);
}
}
Library.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();
}
答案 0 :(得分:1)
您的代码中有几个问题:
使用setBounds
和evil和frowned upon的null-layout
(setLayout(null)
),因为这可能会导致令人讨厌的问题例如this,请使用正确的Layout Managers,以使您的应用程序在所有OS和PLAF中都能正确呈现。
使用public static
成员,可能会导致应用程序流程不一致。
现在,您的问题是您创建了一个全局 textfield
变量,但从未对其进行初始化(因此它是null
)
private JTextField textField;
但是,然后,在构造函数上创建另一个具有相同名称的变量,但这是一个 local 变量:
JTextField textField = new JTextField();
当您拨打此行时:
else if (!Library.users.get(textField.getText()).getUserType().equals(comboBox.getActionCommand())) {
为了解决此问题, Java使用的是 global 变量(记住它是null
),而不是 local 变量(已初始化)。在 local 变量的声明中删除JTextField
:
JTextField textField = new JTextField();
因此,它变成:
textField = new JTextField();
这将初始化全局变量,您将可以在ActionListener