2

I am developing an JavaFX application. It is at its most elementary stages. But I keep getting a strange error, which i cannot manage to fix. I have looked online, and have followed the recommendations. But, the issue is so basic, that i cannot understand what could possibly be the issue. It keeps telling me that the location is required, even though the location is correct and specified.

Caused by: java.lang.NullPointerException: Location is required.

It points to the Parent root.... line for this.

Here is the code i use to load the fxml document:

public static void main(String[] args) {
    launch(args);
}

public void start(Stage primaryStage) throws Exception{
  Parent root = FXMLLoader.load(getClass().getResource("ScheduleView.fxml"));
  primaryStage.setScene(new Scene(root, 500, 500));
  primaryStage.setTitle("Scheduler");
  primaryStage.show();
}

Note that the above code is located in the schedule driver class.

Below is the file struture

This is the file structure of the project

I have tried creating a separate JavaFX project with the exact same FX code/documents, and it works. Any help would be appreciated.

Thanks.

6
  • Have your tried a clean and rebuild? Commented Nov 23, 2017 at 15:08
  • Yes, the issue remains. Commented Nov 23, 2017 at 15:11
  • If you would have maven as dependency management I could help, but I have no idea how can it be done in gradle :( Commented Nov 23, 2017 at 17:14
  • 1
    The easiest but ugly solution would be to move the .fxml file to the resources folder, there is a better solution to tell the build that such files like .fxml must be treated like they are in the resources folder, that can be easy done in maven, i think there should be a similar solution in gradle too. If you know both maven and gradle, I can show you the maven code then you can convert it to gradle if it is posible. Commented Nov 23, 2017 at 17:20
  • @Sunflame I don't think it is ugly solution. Images should be in resources folder. Commented Nov 26, 2017 at 9:58

1 Answer 1

1

put your fxml file into src/main/resources directory.

And load it with (added a slash):

Parent root = FXMLLoader.load(getClass().getResource("/ScheduleView.fxml"));

The directory src/main/java is not on the classpath.

Sign up to request clarification or add additional context in comments.

3 Comments

This won't work since if you have a dependency manager like maven or gradle it creates you a resources folder, and the getClass().getResource(...) is tying to get the file from the resources folder
@Sunflame wrong. maven takes the content of src/main/resources and put it during the build process into the target/classes folder and that's where the classloader looks for it.
Ahh sorry, didn't see the first line of your answer, just the highlighted code, and the only difference was the /, and if it would be in the same place as it is now it would not work, but you're right in resources it works. Sorry for my inattention.But besides that it is not a really good solution to separate in resources your .fxml files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.