请不要将此问题标记为重复,因为它不仅仅是NullPointerEcxeption案例。
即使应用程序正常运行,但控制台仍显示错误消息,我仍在努力尝试理解和解决的一个问题。
这是简单的Java聊天,我正在学习套接字编程。每次我运行我的应用程序或发送消息时,我的应用程序都会显示错误,但是我发送的消息会正确显示:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at chat.client.ClientWindow.lambda$printMsg$0(**ClientWindow.java:88**)
at java.desktop/java.awt.event.InvocationEvent.dispatch$$$capture(InvocationEvent.java:313)
at java.desktop/java.awt.event.InvocationEvent.dispatch(InvocationEvent.java)
at java.desktop/java.awt.EventQueue.dispatchEventImpl(EventQueue.java:764)
at java.desktop/java.awt.EventQueue.access$500(EventQueue.java:97)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:717)
at java.desktop/java.awt.EventQueue$3.run(EventQueue.java:711)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at java.base/java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:89)
at java.desktop/java.awt.EventQueue.dispatchEvent(EventQueue.java:734)
at java.desktop/java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:199)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:124)
at java.desktop/java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:113)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:109)
at java.desktop/java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.desktop/java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
ClientWindow.java:88-是
SwingUtilities.invokeLater(() -> textArea.appendText(msg + "\n"));
这是我的代码,请帮助我了解问题所在。
package chat.server;
import chat.network.TCPConnection;
import chat.network.TCPConnectionListener;
import javafx.application.Application;
import javafx.stage.Stage;
import java.io.IOException;
import java.net.ServerSocket;
import java.util.ArrayList;
public class ChatServer extends Application implements TCPConnectionListener {
public static void main(String[] args) {
new ChatServer();
}
private final ArrayList<TCPConnection> connections = new ArrayList<>();
private ChatServer(){
System.out.println("Server running...");
try(ServerSocket serverSocket = new ServerSocket(8189)) {
while (true){
try {
new TCPConnection(this,serverSocket.accept());
}catch (IOException e){
System.out.println("TCPConnection exception: " + e);
}
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
@Override
public void start(Stage primaryStage) throws Exception {
}
@Override
public void onConnectionReady(TCPConnection tcpConnection) {
connections.add(tcpConnection);
sendToAllConnections("Client connected: " + tcpConnection);
}
@Override
public void onReceiveString(TCPConnection tcpConnection, String value) {
sendToAllConnections(value);
}
@Override
public void onDisconnect(TCPConnection tcpConnection) {
connections.remove(tcpConnection);
sendToAllConnections("Client disconnected: " + tcpConnection);
}
@Override
public void onException(TCPConnection tcpConnection, Exception e) {
System.out.println("TCPConnection exception: " + e);
}
private void sendToAllConnections(String value){
System.out.println(value);
for (TCPConnection connection : connections)
connection.sendString(value);
}
}
package chat.client;
import chat.network.TCPConnection;
import chat.network.TCPConnectionListener;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javax.swing.*;
import java.io.IOException;
public class ClientWindow extends Application implements TCPConnectionListener {
private static final String IP_ADDRES = "192.168.1.190";
private static final int PORT = 8189;
@FXML
private TextArea textArea;
@FXML
private TextField writeArea;
@FXML
private TextField nameArea;
@FXML
private Button send;
TCPConnection connection;
public void buttonClickHandler(ActionEvent evt){
String msg = writeArea.getText();
if(msg.equals("") || msg.equals(null)) return;
writeArea.setText(null);
connection.sendString(nameArea.getText() + ": " + msg);
}
@Override
public void start(Stage primaryStage) throws Exception{
Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
primaryStage.setTitle("Java Chat");
primaryStage.setScene(new Scene(root, 590, 400));
primaryStage.show();
}
public static void main(String[] args) {
launch(args);
SwingUtilities.invokeLater(new Runnable(){
@Override
public void run() {
new ClientWindow();
}
});
}
public ClientWindow(){
try {
connection = new TCPConnection(this, IP_ADDRES, PORT);
} catch (IOException e) {
printMsg("Connection Exception" + e);
}
}
@Override
public void onConnectionReady(TCPConnection tcpConnection) {
printMsg("Connection ready...");
}
@Override
public void onReceiveString(TCPConnection tcpConnection, String value) {
printMsg(value);
}
@Override
public void onDisconnect(TCPConnection tcpConnection) {
printMsg("Connection close...");
}
@Override
public void onException(TCPConnection tcpConnection, Exception e) {
printMsg("Connection Exception" + e);
}
private synchronized void printMsg(String msg){
SwingUtilities.invokeLater(() -> textArea.appendText(msg + "\n"));
}
}
package chat.network;
import java.io.*;
import java.net.Socket;
import java.nio.charset.Charset;
public class TCPConnection {
private final Socket socket;
private final Thread rxThread;
private final TCPConnectionListener eventListener;
private final BufferedReader in;
private final BufferedWriter out;
public TCPConnection(TCPConnectionListener eventListener, String idAddr, int port) throws IOException {
this(eventListener, new Socket(idAddr,port));
}
public TCPConnection(TCPConnectionListener eventListener, Socket socket) throws IOException {
this.eventListener = eventListener;
this.socket = socket;
in = new BufferedReader(new InputStreamReader(socket.getInputStream(), Charset.forName("UTF-8")));
out = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream(), Charset.forName("UTF-8")));
rxThread = new Thread(new Runnable() {
@Override
public void run() {
try {
eventListener.onConnectionReady(TCPConnection.this);
while(!rxThread.isInterrupted()) {
eventListener.onReceiveString(TCPConnection.this,in.readLine());
}
}catch (IOException e){
eventListener.onException(TCPConnection.this, e);
}finally {
eventListener.onDisconnect(TCPConnection.this);
}
}
});
rxThread.start();
}
public synchronized void sendString(String value){
try {
out.write(value + "\r\n");
out.flush();
}catch (IOException e){
eventListener.onException(TCPConnection.this, e);
disconnect();
}
}
public synchronized void disconnect(){
rxThread.interrupt();
try {
socket.close();
} catch (IOException e) {
eventListener.onException(TCPConnection.this, e);
}
}
@Override
public String toString() {
return "TCPConnection: " + socket.getInetAddress() + ": " + socket.getPort();
}
}
package chat.network;
public interface TCPConnectionListener {
void onConnectionReady(TCPConnection tcpConnection);
void onReceiveString(TCPConnection tcpConnection, String value);
void onDisconnect(TCPConnection tcpConnection);
void onException(TCPConnection tcpConnection, Exception e);
}
P.S。我试图制作Java Swing GUI而不是JavaFX。 另外,我没有将fxml文件包含所有元素。