JavaFX Location is not set error message

123,485

Solution 1

I had this problem and found this post. My issue was just a file name issue.

FXMLLoader(getClass().getResource("/com/companyname/reports/" +
report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") +
".fxml"));

Parent root = (Parent) loader.load();

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:

FXMLLoader loader = new FXMLLoader(ReportMenu.this.getClass().getResource("/fxml/" + report.getClass().getCanonicalName().substring(18).replaceAll("Controller", "") + ".fxml"));

Solution 2

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:

FXMLLoader myLoader = new FXMLLoader(getClass().getResource("/createCategory.fxml"));
//                                                           ^

Solution 3

I was getting this exception and the "solution" I found was through Netbeans IDE, simply:

  1. Right-click -> "Clean and Build"
  2. Run project again

I don't know WHY this worked, but it did!

Solution 4

I converted a simple NetBeans 8 Java FXML application to the Maven-driven one. Then I got problems, because the getResource() methods weren't able to find the .fxml files. In mine original application the fxmls were scattered through the package tree - each beside its controller class file. After I made Clean and build in NetBeans, I checked the result .jar in the target folder - the .jar didn't contain any fxml at all. All the fxmls were strangely disappeared.

Then I put all fxmls into the resources/fxml folder and set the getResource method parameters accordingly, for example: FXMLLoader(App.class.getClassLoader().getResource("fxml/ObjOverview.fxml")); In this case everything went OK. The fxml folder appeared int the .jar's root and it contained all my fxmls. The program was working as expected.

Solution 5

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("../view/Main.fxml")); 

in my case i just remove ..

FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/Main.fxml")); 
Share:
123,485
Admin
Author by

Admin

Updated on July 05, 2022

Comments

  • Admin
    Admin almost 2 years

    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.