Issue
I want to implement javafx app both on desktop and android. When I try load fxml file:
FXMLLoader loader = new FXMLLoader(getClass().getResource("ProjectSetup.fxml"));
Parent root = loader.load();
I am getting an error:
java.lang.IllegalStateException: Location is not set.
I tried putting it in Resources(Main), Resources(Desktop),Source Packages(Java).
Example
None of it worked.
Where should I put "ProjecSetup.fxml" to get it working?
Solution
The appropriate location for fxml files is src/main/resources/yourpackage
. If the class which is calling getResource()
is not located in the same package as the referenced fxml.file, i.e. src/main/yourpackage
, you have to use an aboslute path: fxmlLoader(getClass().getResource("/yourpackage/ProjectSetup.fxml")
. Pay attention to the leading slash
Answered By - jns
Answer Checked By - Katrina (JavaFixing Volunteer)