. Advertisement .
..3..
. Advertisement .
..4..
Most of the time, the “Exception in Application start method” error in JavaFX happens when FXMLLoader couldn’t load the resource file properly. Read on to find out why it occurs and how to fix it.
The “Exception in Application start method” Error
Reproduce The Error
Here is an example of an attempt at creating a JavaFX program. At first glance, it should have no difficulty running it. However, such an effort will result in an “Exception in Application start method” error like below.
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
public class Main extends Application {
@Override
public void start(Stage stage) throws Exception {
Parent root = FXMLLoader.load(getClass().getResource("../userInterface/DogFinderFXML.fxml"));
Scene scene = new Scene(root);
stage.setScene(scene);
stage.setTitle("ITTutoria");
stage.show();
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}}
Output:
Exception in Application start method
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$147(Unknown Source)
at com.sun.javafx.application.LauncherImpl$$Lambda$48/1732398722.run(Unknown Source)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
Diagnose The Error
If you examine the error message, you will notice these two lines:
Caused by: java.lang.NullPointerException: Location is required.
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source)
Basically, JavaFX is telling you that the program fails to load an object hierarchy from the XML document you provide. At first, it seems strange because in the code you have clearly included a .fxml file.
Parent root = FXMLLoader.load(getClass().getResource("DogFinderFXML.fxml"));
However, there are many causes that may prevent JavaFX from loading this file properly. They come from a broader mechanism: how resources (such as stylesheets, images, and FXML files) are loaded.
During development, a JavaFX application typically runs from a file system, and at runtime, it loads the required files. Most developers save resources as individual files in the same file system. Therefore, the application should load them just fine.
This isn’t the case when the application has been compiled and built into a .jar file, however. You no longer run your program from the .java source files, and so do the resource files. JavaFX has bundled them into the .jar file, meaning they aren’t individual files on your file system anymore.
You may already know how JavaFX loads resources with URLs. In particular, you will need to create an FXMLLoader object to load .fxml files. You can do so by using the method setLocation()
, the FXMLLoader constructor, or the method FXMLLoader.load()
.
But at the end of the day, they all need a valid java.net.URL
object, which can be created with getResource()
as we have done above. And this method has its own rules when it comes to parsing URLs. When you fail to understand and adhere to them, you will end up with the “Exception in Application start method” error above.
Particularly, if the method getResource()
notices that the location you give it starts with a slash (/), it will refer it to the root of the classpath and the rest of the location will become the relative path to your .fxml file. Otherwise, it will append the package name before the location to form the URL.
Now, look at how our application makes use of the method getResource()
:
Parent root = FXMLLoader.load(getClass().getResource("../userInterface/DogFinderFXML.fxml"));
We have provided “../userInterface/DogFinderFXML.fxml
” as the path to our .fxml file. This isn’t a valid path that can be interpreted correctly by getResource()
because single dots (.)
and double dots (..) aren’t valid Java identifiers. You can’t use them in resource names or JavaFX won’t be able to load this resource and give us an error as a result.
Instead, we should change it to this:
Parent root = FXMLLoader.load(getClass().getResource("/userInterface/DogFinderFXML.fxml"));
The slash (/) at the beginning now refers to the root of the classpath, or src/main/resources, while userInterface refers to the subpackage where the .fxml file is located. Note: read this guide to learn more about calling methods in another class.
Conclusion
The “Exception in Application start method
” error occurs in JavaFX because you have used the wrong location for the method getResource()
. Fix this URL and your program will be able to load resources properly without any problem.
Leave a comment