When I run the main class, in a Maven project, I get:
Exception in Application start method
From what I've understood i'ts because the FXML file won't load.
This is the Filesystem Hierarchy
src
└- main
├- java
| └- zenit
| └- ui
| └- TestUI.java
└- resources
└- zenit
└- ui
└- Main.fxml
In Eclipse (inside src)
Reading apache's maven guide and other statck questions, following code-snippet seems to be the right way to create the FXMLLoader
and point to the Main.fxml
file.
public class TestUI extends Application {
public void start(Stage stage) throws IOException {
FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
.
. // set controller, load parent, set scene etc.
.
}
public static void main(String[] args) {
launch(args);
}
}
But this code will throw the previously stated exception.
When I tried to pinpoint where in my code the Exception
came from I figured out it was form the FXMLLoader
.
So I ran the different sysouts (one at a time), on the line above FXMLLoader loader = new FXMLLoader(..)
:
System.out.println(getClass().getResource("Main.fxml"));
System.out.println(getClass().getResource("main/resources/zenit/ui/Main.fxml"));
System.out.println(getClass().getResource("../../../resources/zenit/ui/Main.fxml"));
For all, the console output was: (obviously not expected output)
null
So, my question are,
Why does .getResource("Main.fxml")
return null
here?
How do I correctly access the Main.fxml
file in this case?
My system:
- Maven: Apache Maven 3.6.0
- Java SE: 11.0.2
- JavaFX: 11.0.2
- Eclipse: 2018-12 (4.10.0)
- OS: macOS Mojave - v10.14.3