I have problem when trying to close current scene and open up another scene when menuItem is selected. My main stage is coded as below:
public void start(Stage primaryStage) throws Exception {
primaryStage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("cartHomePage.fxml"));
Pane myPane = (Pane) myLoader.load();
CartHomePageUI controller = (CartHomePageUI) myLoader.getController();
controller.setPrevStage(primaryStage);
Scene myScene = new Scene(myPane);
primaryStage.setScene(myScene);
primaryStage.show();
}
When the program is executed, it will go to the cartHomePage.fxml. From there, I can select to go to create product or create category when the menu item is selected. Here is my action event:
Stage prevStage;
public void setPrevStage(Stage stage){
this.prevStage = stage;
}
public void gotoCreateCategory(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("createCategory.fxml"));
Pane myPane = (Pane) myLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
setPrevStage(stage);
stage.show();
}
//Method to change scene when menu item create product is on click
@FXML
public void gotoCreateProduct(ActionEvent event) throws IOException {
Stage stage = new Stage();
stage.setTitle("Shop Management");
FXMLLoader myLoader = new FXMLLoader(getClass().getResource("creatProduct.fxml"));
Pane myPane = (Pane) myLoader.load();
Scene scene = new Scene(myPane);
stage.setScene(scene);
prevStage.close();
setPrevStage(stage);
stage.show();
}
However, I can only switch the stage once. For example, my default page is cartHomePage.fxml. When I run the program, first I go to create product stage. After that, I cannot go to anywhere any more. The error message is:
java.lang.IllegalStateException: Location is not set.
and Null Pointer Exception
I did set the stage after I close it and pass it around. I wonder which part went wrong.
Thanks in advance.
I think problem is either incorrect layout name or invalid layout file path.
for IntelliJ, you can create resource directory and place layout files there.
I was getting this exception and the "solution" I found was through Netbeans IDE, simply:
I don't know WHY this worked, but it did!
I know this is a late answer, but I hope to help anyone else who has this problem. I was getting the same error, and found that I had to insert a
/
in front of my file path. The corrected function call would then be:That happened to me an i found the solution. If u build your project with your .fxml files in different packages from the class that has the launch line (
Parent root = FXMLLoader.load(getClass().getResource("filenamehere.fxml"));
) and use a relative path your windows except from the first one wont launch when your run the jar. To keep it short place the .fxml file in the same package with the class that launches it and set the path like this ("filenamehere.fxml") and it should work fine.I mean something like this:
After that
Try it and let me know please.
I had the same problem.
after a few minutes, i figured it that i was trying the load the file.fxml from wrong location.