我正在处理的聊天客户端的代码会生成以下错误:
"Exception in thread "Thread-0" java.lang.NullPointerException at ChatClient$FieldLengthChecker.run(ChatClient.java:26)"
此错误仅发生大约 50% 的时间。
另外 50% 的时间,程序按预期运行。
该线程用于“侦听” userAuthField 中的字符,以确保用户在我设置 authLoginButton.setEnabled(true) 之前至少输入了 4 个字符。
如果 authUserField 内容的长度低于 4 个字符,则应禁用该按钮。
如果我运行程序并在字段中输入 4 个字符,它要么给我上面列出的异常,要么运行良好,直到我关闭应用程序(如,我可以添加和删除字符,按钮将按预期激活/停用,直到我关闭程序)。
非常感谢任何和所有的见解。
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.net.*;
public class ChatClient {
JFrame authFrame;
JPanel authPanel;
JLabel authUserLabel;
JLabel authPassLabel;
JLabel authMessageLabel;
JTextField authUserField;
JTextField authPassField;
JButton authLoginButton;
JButton authRegisterButton;
String authUserData;
String authPassData;
//This inner-class is used in a thread to listen
//for characters entering and exiting the userAuthField
//in order to change the status of authLoginButton
public class FieldLengthChecker extends Thread {
public void run() {
while(true) {
String username = authUserField.getText();
if (username.length() > 3) {
authLoginButton.setEnabled(true);
}
else {
authLoginButton.setEnabled(
}
}
}
}
public static void main(String[] args) {
ChatClient chat = new ChatClient();
chat.start();
}
public void start() {
FieldLengthChecker FieldLengthChecker = new FieldLengthChecker();
authFrame = new JFrame("Authentication");
authFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
authPanel = new JPanel();
authUserLabel = new JLabel("Username");
authPassLabel = new JLabel("Password");
authMessageLabel = new JLabel();
authUserField = new JTextField(20);
authPassField = new JTextField(20);
authLoginButton = new JButton("Login");
authLoginButton.addActionListener(new AuthLoginButtonListener());
authLoginButton.setEnabled(false);
authRegisterButton = new JButton("Register");
authFrame.getContentPane().add(authPanel);
authPanel.add(authUserLabel);
authPanel.add(authUserField);
authPanel.add(authPassLabel);
authPanel.add(authPassField);
authPanel.add(authLoginButton);
authPanel.add(authMessageLabel);
authFrame.setSize(250, 300);
authFrame.setVisible(true);
FieldLengthChecker.start();
}
//This inner-class takes the user and pass information from
//authUserField and authPassField and sends it to the
//Authentication server to query the associated database
class AuthLoginButtonListener implements ActionListener {
Socket authSock;
BufferedReader authReader;
PrintWriter authWriter;
public void actionPerformed(ActionEvent event) {
try {
authSock = new Socket("127.0.0.1", 5001);
InputStreamReader streamReader = new InputStreamReader(authSock.getInputStream());
authReader = new BufferedReader(streamReader);
authWriter = new PrintWriter(authSock.getOutputStream());
System.out.println("Connection established with Authentication server");
authUserData = authUserField.getText();
authPassData = authPassField.getText();
authWriter.println(authUserData + " " + authPassData);
authWriter.close();
}
catch(Exception ex) {
ex.printStackTrace();
System.out.println();
System.out.println("Failed to establish a connection with the authentication server.");
System.out.println("See above stack trace for further information.");
authFrame.dispose();
}
}
}
}