I am trying to load an FXML file:
URL url = getClass().getClassLoader().getResource("/frontEnd/fxml/ModeScreen.fxml");
try {
this.value = FXMLLoader.load(url);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
But when I run, I get:
NullPointerException: Location is required
the path to my fxml file is:
src/frontEnd/fxml/ModeScreen.fxml
and the path to my class file is:
src/frontEnd/ModeScreen.java
this.value extends AnchorPane
and the FXML file's root is an AnchorPane.
I checked and made sure that all of these files are in the bin folder and it doesn't run in either a jar file or Eclipse
I have also tried to use the following paths:
frontEnd/fxml/ModeScreen.fxml
../frontEnd/fxml/ModeScreen.fxml
src/frontEnd/fxml/ModeScreen.fxml
/src/frontEnd/fxml/ModeScreen.fxml
/fxml/ModeScreen.fxml
fxml/ModeScreen.fxml
I have also tried using getResourceAsStream.toString
file structure is as follows:
bin
application
Main
frontEnd
controllers
fxml<---<all the fxml files are in here. I checked too.>
ModeScreen.class
There must be some tiny thing I am doing wrong.
The problem in here is that you are appending a /
, when you are already using a ClassLoader
's getResource()
.
You should never use a /
at the beginning of a ClassLoader path is because all ClassLoader paths are absolute
.
You can definitely use Class
's getResource()
with a URL which starts with /
, because before it delegates down to the classloader, it creates an absolute URL.
From the JavaDocs :
Before delegation, an absolute resource name is constructed from the given resource name using this algorithm:
- If the name begins with a '/' ('\/'), then the absolute name of the resource is the portion of the name following the '/'.
The following are valid url :
Using ClassLoader :
URL url = getClass().getClassLoader().getResource("frontEnd/fxml/ModeScreen.fxml");
Using Class :
URL url = getClass().getResource("/frontEnd/fxml/ModeScreen.fxml");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.