切换屏幕时出错 Java Fx FXML

Error switching screen Java Fx FXML

Hello. I'm kinda stuck.
I got an error, I'm not able to close my previous window to set the new one.
Here is my controller :

public class Controller implements Initializable {

//------------- init Order  -------------//
@FXML
private Label text10;
@FXML
private Label text20;
@FXML
private Label text30;
@FXML
private Label text40;
@FXML
private Label text50;

@FXML
private TextField entry10;
@FXML
private TextField entry20;
@FXML
private TextField entry30;

@FXML
private Button btnConnect10;

//------------- init Reception  -------------//
@FXML
private Label text11;
@FXML
private Label text21;
@FXML
private Label text31;
@FXML
private Label text41;
@FXML
private Label text51;

@FXML
private TextField entry11;
@FXML
private TextField entry21;
@FXML
private TextField entry31;

@FXML
private Button btnConnect11;

//------------- init Connexion  -------------//
@FXML
private ImageView img;
@FXML
private Label user;
@FXML
private Label password;
@FXML
private TextField usr;
@FXML
private PasswordField passwd;
@FXML
private Button btnConnect13;

//------------- init popup  -------------//
@FXML
private Label popup;

//------------- Stage   -------------//
Stage prevStage;

public void setPrevStage(Stage stage) {
    this.prevStage = stage;
}

@FXML
private void buttonPressed(KeyEvent e) {
    if (e.getCode() == KeyCode.ENTER) {
        System.out.println("je suis un krab");
    }
}

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
    //init classe
    Controller ctr = new Controller();

    //fenetre Commande
    if (event.getSource() == btnConnect10) {
        ctr.Commande(entry10.getText(), entry20.getText(), entry30.getText());
    }
    //fenetre Reception
    if (event.getSource() == btnConnect11) {
        ctr.Reception(entry11.getText(), entry21.getText(), entry31.getText());
    }

    //fenetre connexion      
    if (event.getSource() == btnConnect13) {
        ctr.Connect(usr.getText(), passwd.getText());
    }
}

//Menu d'affchage
private void menu(int access) throws IOException {
    //init fenetre
    Stage stage = new Stage();
    Pane myPane = null;
    Scene scene;

    switch (access) {

        case 1:
            stage.setTitle("Commande OR tool");               
            myPane = FXMLLoader.load(getClass().getResource("CommandeFXML.fxml"));
            scene = new Scene(myPane);
            stage.setScene(scene);
            prevStage.close();
            stage.show();   
            break;

        case 2:
            //recupère les infos de la fenetre
            stage.setTitle("Réception OR tool");
            myPane = FXMLLoader.load(getClass().getResource("ReceptionFXML.fxml"));
            scene = new Scene(myPane);
            stage.setScene(scene);
            prevStage.close();
            stage.show();   
            break;

        case 3:                
            stage.setTitle("Admin OR tool");
            myPane = FXMLLoader.load(getClass().getResource("AdminFXML.fxml"));
            scene = new Scene(myPane);
            stage.setScene(scene);
            prevStage.close();
            stage.show();              
            break;

        default:
            //error
            System.out.println("erreur changing window");
    }
}private void Connect(String getuser, String getpasswd) throws IOException {
    ConnectBDD bdd = new ConnectBDD();
    Controller ctr = new Controller();

    System.out.println("user = "+getuser+"\npassword ="+getpasswd);

    //test 
    if (!getuser.isEmpty() && !getpasswd.isEmpty()) {
        //check if user exist
        if (bdd.connectUserTest(getuser, getpasswd) == true) {
            //get the access level
            int acces = bdd.connectUser (getuser, getpasswd);

            //Changing window
            ctr.menu(acces);
        } 
        else {
            System.out.println("user not in the data base");
        }

    } //message d'erreur 
    //to do faire un pop up
    else {
        System.out.println("no entry");
    }
}

And the main

public class JavaFX extends Application {

@FXML
@Override
public void start(Stage primaryStage) throws Exception {

    primaryStage.setTitle("OR tool");

    FXMLLoader myLoader = new FXMLLoader(getClass().getResource("ConnectFXML.fxml"));

    Pane myPane = (Pane) myLoader.load();

    Controller controller = (Controller) myLoader.getController();

    controller.setPrevStage(primaryStage);

    Scene myScene = new Scene(myPane);
    primaryStage.setScene(myScene);
    primaryStage.show();

}

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    launch(args);
}

I got a nice stack

Exception in thread "JavaFX Application Thread" java.lang.RuntimeException: java.lang.reflect.InvocationTargetException
at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1774)
at javafx.fxml.FXMLLoader$ControllerMethodEventHandler.handle(FXMLLoader.java:1657)
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.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:49)
at javafx.event.Event.fireEvent(Event.java:198)
at javafx.scene.Node.fireEvent(Node.java:8411)
at javafx.scene.control.Button.fire(Button.java:185)
at com.sun.javafx.scene.control.behavior.ButtonBehavior.mouseReleased(ButtonBehavior.java:182)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:96)
at com.sun.javafx.scene.control.skin.BehaviorSkinBase.handle(BehaviorSkinBase.java:89)
at com.sun.javafx.event.CompositeEventHandler$NormalEventHandlerRecord.handleBubblingEvent(CompositeEventHandler.java:218)
at com.sun.javafx.event.CompositeEventHandler.dispatchBubblingEvent(CompositeEventHandler.java:80)
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.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$MouseHandler.process(Scene.java:3757)
at javafx.scene.Scene$MouseHandler.access00(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:380)
at com.sun.javafx.tk.quantum.GlassViewEventHandler$MouseEventNotification.run(GlassViewEventHandler.java:294)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.lambda$handleMouseEvent4(GlassViewEventHandler.java:416)
at com.sun.javafx.tk.quantum.QuantumToolkit.runWithoutRenderLock(QuantumToolkit.java:389)
at com.sun.javafx.tk.quantum.GlassViewEventHandler.handleMouseEvent(GlassViewEventHandler.java:415)
at com.sun.glass.ui.View.handleMouseEvent(View.java:555)
at com.sun.glass.ui.View.notifyMouse(View.java:937)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null8(WinApplication.java:191)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.Trampoline.invoke(MethodUtil.java:71)
    at sun.reflect.GeneratedMethodAccessor1.invoke(Unknown Source)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.reflect.misc.MethodUtil.invoke(MethodUtil.java:275)
    at javafx.fxml.FXMLLoader$MethodHandler.invoke(FXMLLoader.java:1769)
    ... 48 more

The most important part is

Caused by: java.lang.NullPointerException
at javafx.Controller.menu(Controller.java:149)
at javafx.Controller.Connect(Controller.java:303)
at javafx.Controller.handleButtonAction(Controller.java:130)

ligne 149 is when i try to close the previous windows. "prevStage.close();" in the menu function of the controller
ligne 303 is the call of the fuction menu in the test of connexion.
ligne 130 is in the handleButtonAction when i call the connect function

When I try to open a new window in the handler it works perfectly. But I need to adpat the window to the access level of the user.
But now I've got a java.lang.NullPointerException.

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {//init fenetre
    Stage stage = new Stage();
    Pane myPane = null;
    Scene scene;stage.setTitle("Réception OR tool");
            myPane = FXMLLoader.load(getClass().getResource("ReceptionFXML.fxml"));
            scene = new Scene(myPane);
            stage.setScene(scene);
            prevStage.close();
            stage.show(); }

您在代码中重复创建新的控制器实例:

Controller ctr = new Controller();

对于这些新实例,不会初始化任何内容:您永远不会在这些实例上调用 setPrevStage(),因此 prevStage 不会被初始化,并且由于这些实例不是由 [=14= 创建的], @FXML-注释字段将不会被初始化。

删除对 Controller 构造函数的调用,并在当前实例上调用方法:

@FXML
private void handleButtonAction(ActionEvent event) throws IOException {
    // remove this:
    //Controller ctr = new Controller();

    //fenetre Commande
    if (event.getSource() == btnConnect10) {
        // replace this
        // ctr.Commande(entry10.getText(), entry20.getText(), entry30.getText());
        // with this
        Commande(entry10.getText(), entry20.getText(), entry30.getText());
        // or equivalently
        // this.Commande(entry10.getText(), entry20.getText(), entry30.getText());
    }
    //fenetre Reception
    if (event.getSource() == btnConnect11) {
       Reception(entry11.getText(), entry21.getText(), entry31.getText());
    }

    //fenetre connexion      
    if (event.getSource() == btnConnect13) {
        Connect(usr.getText(), passwd.getText());
    }
}

在您的整个代码中也是如此。