JAVAFX应用程序线程Nullpointer异常错误(我需要在FX线程上运行)

时间:2018-03-30 21:09:50

标签: java javafx error-handling

基本上我的问题是如果我从另一个fxml文件打开一个新窗口,是否需要通过调用Platform.runlater

在FX线程上启动它

私人聊天类 - 这是通过调用中介来创建的,该中介通过创建新的私人聊天类对象来打开私人聊天窗口。

public PrivateChatFrame(final Mediator mediator, final User user, final ImageLoader imageLoader,
                        final Settings settings,
                        final ErrorHandler errorHandler) {
    Validate.notNull(mediator, "Mediator can not be null");
    Validate.notNull(user, "User can not be null");
    Validate.notNull(imageLoader, "Image loader can not be null");
    Validate.notNull(settings, "Settings can not be null");
    //Validate.notNull(swingMessages, "Swing messages can not be null");
    Validate.notNull(errorHandler, "Error handler can not be null");

    try {

        newPrivStage = new Stage();
        FXMLLoader loader = new FXMLLoader(getClass().getResource("/net/usikkert/kouchat/ui/swing/ChatRoom.fxml"));
        Parent root = loader.load();
        newPrivStage.setTitle("Flake");
        newPrivStage.setScene(new Scene(root, 416, 498));
        this.chatRoomController = loader.getController();
        System.out.println("Private Chat Frame Made");
        newPrivStage.show();

        newPrivStage.setOnCloseRequest(new EventHandler<javafx.stage.WindowEvent>() {
            @Override
            public void handle(javafx.stage.WindowEvent e) {
                newPrivStage.hide();

            }
        });

    }
    catch(Exception e){
        System.out.println("Catch");
        e.printStackTrace();
    }

    msgTF = chatRoomController.getMessageArea();

    this.mediator = mediator;
    this.user = user;
    messageList = chatRoomController.getMessageList();
    messageList.setItems(messages);
    statusIcons = new StatusIcons(imageLoader);
    me = settings.getMe();
    user.setPrivchat(this);
    isVisible = false;

    //updateUserInformation();

    final FileTransferHandler fileTransferHandler = new FileTransferHandler(this);
    fileTransferHandler.setMediator(mediator);


    cmdHistory = new CommandHistory();
} 

创建私人聊天方法的方法

@Override
    public void createPrivChat(final User user) {

        if (user.getPrivchat() == null) {

                    user.setPrivchat(new PrivateChatFrame(SwingMediator.this, user, imageLoader,
                                                          settings,  errorHandler));
                }


        if (user.getPrivateChatLogger() == null) {
            user.setPrivateChatLogger(new ChatLogger(user.getNick(), settings, errorHandler));
        }
    }

这是我收到的错误消息。我启动窗口时是否需要Platform.runLater?我认为,因为窗口就像是已经启动的fxml窗口中的一个单独的实例,所以它们不会在同一个线程上

Mar 30, 2018 1:57:07 PM net.usikkert.kouchat.util.UncaughtExceptionLogger uncaughtException
SEVERE: UncaughtException in thread: JavaFX Application Thread (id 15, priority 5)
java.lang.NullPointerException
    at net.usikkert.kouchat.ui.swing.PrivateChatFrame.<init>(PrivateChatFrame.java:129)
    at net.usikkert.kouchat.ui.swing.SwingMediator.createPrivChat(SwingMediator.java:830)
    at net.usikkert.kouchat.ui.swing.SwingMediator.showPrivChat(SwingMediator.java:848)
    at net.usikkert.kouchat.ui.swing.SidePanel.lambda$null$0(SidePanel.java:168)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$147(WinApplication.java:177)
    at java.lang.Thread.run(Thread.java:748)

0 个答案:

没有答案