0

我的应用程序有一个登录场景和一个主视图场景,发生的事情是当我登录时调用 MainView 有时我得到这个异常:

java.lang.NullPointerException
    at javafx.scene.Scene.focusInitial(Scene.java:1879)
    at javafx.scene.Scene.access$3600(Scene.java:170)
    at javafx.scene.Scene$ScenePulseListener.focusCleanup(Scene.java:2181)
    at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2221)
    at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:363)
    at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:460)
    at com.sun.javafx.tk.quantum.QuantumToolkit$9.run(QuantumToolkit.java:329)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.access$100(WinApplication.java:29)
    at com.sun.glass.ui.win.WinApplication$3$1.run(WinApplication.java:73)
    at java.lang.Thread.run(Thread.java:722)

奇怪的是它并不总是发生,只是有时。

我的课:

public class TargetAppDesktop extends Application {

    @Override
    public void start(Stage primaryStage) throws IOException {

        Scene scene = new Scene(new AnchorPane());

        LoginManager loginManager = new LoginManager(scene);
        loginManager.showLoginScreen();
        primaryStage.setResizable(false);
        primaryStage.setScene(scene);
        primaryStage.show();

        primaryStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
            @Override
            public void handle(WindowEvent t) {
                MainViewController.deleteTempFiles();
                Platform.exit();
                System.exit(0);
            }
        });

    }

    public static void main(String[] args) {
        launch(args);
    }
}

我的登录管理器课程

public class LoginManager {

    private Scene scene;

    LoginManager(Scene scene) {
        this.scene = scene;
    }

    public void logout() {
        showLoginScreen();
    }

    void showLoginScreen() {
        try {
            FXMLLoader loader = new FXMLLoader(
                    getClass().getResource("Login.fxml"));
            //         scene.getStylesheets().add(this.getClass().getResource("Login.css").toExternalForm());

            scene.setRoot((Parent) loader.load());
            LoginController controller =
                    loader.<LoginController>getController();
            controller.initManager(this);
        } catch (IOException ex) {
            Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    void showMainViewScreen(Login loginTargetApp, Login loginGateway, Gateway gateway, File file, ArrayList<Integer> anoList) {

            try {

                FXMLLoader loader = new FXMLLoader(getClass().getResource("MainView.fxml"));

                scene.setRoot((Parent) loader.load());

                MainViewController controller = loader.<MainViewController>getController();

                controller.initSessionID(this, scene, loginTargetApp, loginGateway, gateway, file, anoList);

            } catch (Exception ex) {
                Logger.getLogger(LoginManager.class.getName()).log(Level.SEVERE, null, ex);
            }

    }

    void autheticated(Login loginTargetApp, Login loginGateway, Gateway gateway, File file, ArrayList<Integer> anoList) {
        showMainViewScreen(loginTargetApp, loginGateway, gateway, file, anoList);
    }
}
4

1 回答 1

0

之所以出现这个问题,是因为我试图在另一个线程中更改我的场景,但必须在 Javafx 主线程中进行更改,所以一个简单的 Platform.runLater 解决了我的问题。

您可以在此处找到更多详细信息。(JIRA 链接)

于 2013-06-28T16:51:35.733 回答