我正在开发一个客户端-服务器信使应用程序以供练习。在我的客户端模块中有 Client 类、ClientController 类、ClientGUI 类和 fxml 文件、Message 类、一个 CSS 文件和 Main 类。我的 ClientGUI 类和 ClientController 类中有一个方法 display(String message) ,它将一个 Message 对象添加到具有 id #messages 的 VBox 中。
这是我的问题:在我的 ClientController 类中有一个 display() 方法,它调用 ClientGUI 的 display() 方法(我知道这似乎是多余的,但这不是问题)。当在我的 ClientGUI 类中调用 display() 方法时,例如在 setOnMouseClick() 方法中,它可以正常工作。但是,当从我的 ClientController 类调用此方法时,我得到一个 NullPointerException,它指向我的 ClientGUI 类的 display() 方法中的 VBox 类型的消息变量。我试过 messages.getChildren().add(new Message(message));
用VBox test = messages;
后者工作正常,而前者抛出异常。我还尝试在我的消息变量上调用其他方法,所有这些方法都会引发相同的错误。为什么会这样?
(它仍处于开发过程中,因此可能存在其他不相关的问题)
客户:
package client;
import java.io.*;
import java.net.Socket;
/**
* Gabe Castelli
* 9/26/2016
* Description: Represents client and its socket
*/
public class Client implements Serializable {
private static Client instance;
private Socket clientSocket;
private BufferedReader in;
private PrintWriter out;
private ObjectOutputStream objectOutputStream;
private boolean connected = false;
private static final long serialVersionUID = 0L;
private Client() {}
public void connect(String targetAddress, String username_, int port_) {
try {
clientSocket = new Socket(targetAddress, port_);
clientSocket.setReuseAddress(true); // Allows same port to be used successively without cool-down
in = new BufferedReader(new InputStreamReader(clientSocket.getInputStream()));
out = new PrintWriter(clientSocket.getOutputStream(), true);
objectOutputStream = new ObjectOutputStream(clientSocket.getOutputStream());
objectOutputStream.writeObject(username_);
ClientController.getInstance().display("Attention: Successfully connected to server");
connected = true;
} catch (IOException e) {
ClientController.getInstance().display("Attention: Unable to connect to server");
}
}
public void disconnect() {
try {
if (in != null) {
in.close();
}
if (out != null) {
out.close();
}
if (objectOutputStream != null) {
objectOutputStream.close();
}
if (clientSocket != null) {
clientSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
}
connected = false;
}
public boolean connected() {
return connected;
}
public static Client getInstance() {
if (instance == null) {
instance = new Client();
}
return instance;
}
public void sendMessage(String message) {
out.println(message);
}
}
客户端控制器:
package client;
import javafx.scene.layout.VBox;
/**
* Gabe Castelli
* 9/28/2016
* Description:
*/
public class ClientController implements Controller {
private static ClientController instance;
private ClientController() {}
public static ClientController getInstance() {
if (instance == null) {
instance = new ClientController();
}
return instance;
}
// Controls
public void display(String message) {
ClientGUI.getInstance().display(message);
}
public void displayAndSend(String message) {
display(message);
Client.getInstance().sendMessage(message);
}
public void connect(String address_, String username_, int port_) {
Client.getInstance().connect(address_, username_, port_);
}
public void disconnect() {
Client.getInstance().disconnect();
}
}
客户端GUI:
package client;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
/**
* Gabe Castelli
* 9/26/2016
* Description: GUI for client-side using JavaFX
*/
public class ClientGUI extends Application {
private Scene scene;
private TextField targetAddress;
private TextField fieldUsername;
private TextField fieldPort;
private VBox messages;
private TextField input;
private Button btnConnect;
private String username = "Anonymous";
private int port = 2000;
private static ClientGUI instance;
public static ClientGUI getInstance() {
if (instance == null) {
instance = new ClientGUI();
}
return instance;
}
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("ClientGUI.fxml"));
scene = new Scene(root, 480, 360);
stage.setTitle("MyMessenger");
stage.setScene(scene);
stage.setMinWidth(660);
stage.setMinHeight(495);
stage.setMaxWidth(990);
stage.show();
targetAddress = (TextField) scene.lookup("#targetAddress");
fieldUsername = (TextField) scene.lookup("#fieldUsername");
fieldPort = (TextField) scene.lookup("#fieldPort");
messages = (VBox) scene.lookup("#messages");
input = (TextField) scene.lookup("#input");
btnConnect = (Button) scene.lookup("#btnConnect");
input.setOnKeyPressed(event -> {
if (event.getCode() == KeyCode.ENTER) {
if (Client.getInstance().connected()) {
ClientController.getInstance().displayAndSend(input.getText());
input.setText("");
} else {
display("Attention: Not connected to server");
input.setText("");
}
}
});
btnConnect.setOnMouseClicked(event -> {
if (!Client.getInstance().connected()) {
if (!targetAddress.getText().equals("")) {
if (!fieldUsername.getText().equals("")) {
username = fieldUsername.getText();
}
if (!fieldPort.getText().equals("")) {
port = Integer.valueOf(fieldPort.getText());
}
ClientController.getInstance().connect(targetAddress.getText(), username, port);
btnConnect.setText("Disconnect");
} else {
display("Attention: Address required");
}
} else {
ClientController.getInstance().disconnect();
display("Attention: Disconnecting...");
btnConnect.setText("Connect");
}
});
}
public void display(String message) {
messages.getChildren().add(new Message(message));
}
}
堆栈跟踪:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at client.ClientGUI.display(ClientGUI.java:96)
at client.ClientController.display(ClientController.java:26)
at client.Client.connect(Client.java:34)
at client.ClientController.connect(ClientController.java:35)
at client.ClientGUI.lambda$start$1(ClientGUI.java:81)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:86)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:238)
at com.sun.javafx.event.EventHandlerManager.dispatchBubblingEvent(EventHandlerManager.java:191)
at com.sun.javafx.event.CompositeEventDispatcher.dispatchBubblingEvent(CompositeEventDispatcher.java:59)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:58)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.BasicEventDispatcher.dispatchEvent(BasicEventDispatcher.java:56)
at com.sun.javafx.event.EventDispatchChainImpl.dispatchEvent(EventDispatchChainImpl.java:114)
at com.sun.javafx.event.EventUtil.fireEventImpl(EventUtil.java:74)
at com.sun.javafx.event.EventUtil.fireEvent(EventUtil.java:54)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Scene$ClickGenerator.postProcess(Scene.java:3470)
at javafx.scene.Scene$ClickGenerator.access$8100(Scene.java:3398)
at javafx.scene.Scene$MouseHandler.process(Scene.java:3766)
at javafx.scene.Scene$MouseHandler.access$1500(Scene.java:3485)
at javafx.scene.Scene.impl_processMouseEvent(Scene.java:1762)
at javafx.scene.Scene$ScenePeerListener.mouseEvent(Scene.java:2494)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:352)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:275)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent$355(GlassViewEventHandler.java:388)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:387)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)