我的程序有2个控制器,关闭一个控制器后,我想在TextArea中显示一些String。但是在调用方法后,我在Javafx Thread中得到了NullPointerException。我已经尝试了所有方法,是的,FXML变量已正确分配。
public void showIt(String message) {
try{
Platform.runLater(()->chatArea.appendText(message +"\n"));
} catch(NullPointerException exc) {
exc.printStackTrace();
}
}
从第二类中调用显示方法的方法:
@FXML
void tryToConnect(ActionEvent event) throws IOException, InterruptedException {
Thread t = new Thread(() -> {
Socket socket = null;
try {
socket = new Socket("localhost", 6666);
connectionStabilizer = new ConnectionStabilizer();
connectionStabilizer.getConnection(socket);
} catch (IOException e) {
e.printStackTrace();
}
});
t.start();
MainWindowController mainWindowController = new MainWindowController();
mainWindowController.showIt("works");
cancelConnectionPanel(event);
}
错误消息:
Exception in thread "JavaFX Application Thread" java.lang.NullPointerException
at sample.Controller.lambda$setClockLabel$1(Controller.java:60)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$9(PlatformImpl.java:418)
at java.base/java.security.AccessController.doPrivileged(Native Method)
at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:417)
at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:175)
at java.base/java.lang.Thread.run(Thread.java:844)