我正在处理的聊天客户端的代码会产生以下错误:
"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();
}
}
}
}
答案 0 :(得分:-1)
尝试这样的事情?
public class FieldLengthChecker extends Thread {
public void run() {
while(true) {
try {
String username = authUserField.getText();
if (username.length() > 3) {
authLoginButton.setEnabled(true);
} else {
authLoginButton.setEnabled(false);
}
} catch (NullPointerException e) {
// handle exception
}
}
}
}