I use FXMLLOADER to load a fxml file SignInUI.fxml
in LogUIController
. The code is here:
Stage signIn = new Stage();
FXMLLoader fxmlLoader = new FXMLLoader(HelloApplication.class.getResource("SignInUI.fxml"));
SignInUIController signInUIController = new SignInUIController();
signInUIController.setStage(signIn);
Scene sceneSignIn = new Scene(fxmlLoader.load());
signIn.setTitle("Sign In");
Image icon = new Image("calculator.jpg");
signIn.getIcons().add(icon);
signIn.setScene(sceneSignIn);
signIn.show();
I wrote a method called setStage
in SignInUIController
, which can assign the instance variable stage
:
public Stage stage;
public void setStage(Stage stage) {
this.stage = stage;
}
I tried to build a SignInUIController
instance in LogUIController
and call the setStage
method. Lastly, the cancel
method in SignInUIController
tied to a button and use the instance variable stage
to close the stage:
@FXML
private void cancel() throws IOException {
stage.close();
}
But every time, it has an error:Cannot invoke "javafx.stage.Stage.close()" because "this.stage" is null
. I do not know why, and how to fix this?