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.
For Intellij users, my issue was that the directory where I had my fxml files (src/main/resources), was not marked as a "Resources" directory.
Open up the module/project settings go to the sources tab and ensure that Intellij knows that the directory contains project resource files.
I had the same problem. It's a simple problem of not specifying the right path.
Right click on the on your
.fxml
file and select properties (for those using eclipse won't differ that much for another IDE) and then copy the copy the location starting from/packagename
till the end and that should solve the problemI had this problem and found this post. My issue was just a file name issue.
I have an xml that this is all coming from and I have made sure that my class is the same as the fxml file less the word controller.
I messed up the substring so the path was wrong...sure enough after I fixed the file name it worked.
To make a long story short I think that the problem is either the filename is named improperly or the path is wrong.
ADDITION: I have since moved to a Maven Project. The non Maven way is to have everything inside of your project path. The Maven way which was listed in the answer below was a bit frustrating at the start but I made a change to my code as follows:
This is often not getting the location path correct. It is important to realize that the path starts from the current package which the code resides in and not the root of the project. As long as you get this relative path correct, you should be able to steer clear of this error in this case.
I've had the same issue in my JavaFX Application. Even more weird: In my Windows developement environment everything worked fine with the fxml loader. But when I executed the exact same code on my Debian maschine, I got similar errors with "location not set".
I read all answers here, but none seemed to really "solve" the problem. My solution was easy and I hope it helps some of you:
Maybe Java gets confused, by the getClass() method. If something runs in different threads or your class implements any interfaces, it may come to the point, that a different class than yours is returned by the getClass() method. In this case, your relative path to creatProduct.fxml will be wrong, because your "are" not in the path you think you are...
So to be on the save side: Be more specific and try use the static class field on your Class (Note the YourClassHere.class).
After realizing this, I will ALWAYS do it like this. Hope that helps!