I am creating a JavaFX GUI application using IntelliJ, with the project being built with Maven. I have recently been attempting to create a function to switch between two scenes upon the click of a button. The Java code for the function is as follows:
public void showNewSetPage() {
FXMLLoader loader = new FXMLLoader(getClass().getResource("newSetPage.fxml"));
Scene scene = null;
try {
scene = new Scene(loader.load());
} catch (Exception e) {
e.printStackTrace();
}
Stage stage = new Stage();
stage.setScene(scene);
stage.show();
}
Upon running this code however, it returns this error: java.lang.IllegalStateException: Location is not set.
It also states that the problem in the line scene = new Scene(loader.load());
.
Below is a screenshot of how I have organized my files:
File structure
The function showNewSetPage()
belongs to the homePageController class.
Here is a link to a Google Drive folder which contains all the project source code files:
https://drive.google.com/drive/folders/1RftQHI-NVT3cfBnlzfFclkHB3IJflp8H?usp=drive_link
I know that this is a common problem that many other people have faced when trying to create a function to switch scenes in JavaFX. So far to solve the problem, I have only tried solutions related to changing the string that is inserted into the getResource()
function to be used as a URL for the load()
function.
In the code that I pasted above, I have written FXML file location as “newSetPage.fxml”, however I have also tried:
“/newSetPage.fxml” — (adding a preceding forward slash)
“/Users/damondsylva/Desktop/Coding Projects/Java/CompSci IA Flashcards App/src/main/resources/org/example/compsci_ia_flashcards_app/newSetPage.fxml” — (absolute path)
“src/main/resources/org/example/compsci_ia_flashcards_app/newSetPage.fxml” — (content root)
“org/example/compsci_ia_flashcards_app/newSetPage.fxml” — (source root)
However, all of these strings have pointed to null locations.
I think the problem may be related to the way my project files are structured with Maven, and that I may have to change something in the pom.xml file, but have not been able to understand how it works fully and I’m scared to change anything in that file in fear of creating a bigger problem. All the solutions that I have looked at so far have always cited something about changing the structure of location/URL strings (e.g: adding a preceding forward slash), but as I mentioned before, none of those changes worked.
DAMONster is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.