Important Java Concept

Fix JavaFX Exception in Application Start Method

We will learn how to fix the JavaFX exception in the application start method.

Before going into the concept, a quick look at JavaFX.
JavaFX is a software platform used for creating and delivering desktop applications. JavaFX is a set of graphics and media packages that enables developers to design, create, test, debug, and deploy rich client applications that operate consistently across diverse platforms.

Exception in the Application Start method is the runtime error that occurs when the application is running and when compilation gets done. This state occurs when the application is inefficient in loading runtime variables or files.

JavaFX code

package application; 
         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("SampleXML.fxml")); 
                  Scene scene = new Scene(root); 
                  stage.setScene(scene); 
                  stage.setTitle("hello"); 
                  stage.show(); 
             } 
public static void main(String[] args) { 
                  launch(args); 
             } 
        }

Explanation

In the above block of code, the main method is written inside the class ‘Main’ which extends an abstract Application class. Inside the main method, we have written launch(args);, this statement uses the launch() method which is a static method located in the Application class. This method will detect from which class it is called, so you don’t have to tell what class to launch.

public void start(Stage stage) throws Exception

The above statement is the main entry point for all JavaFX applications. This method is called on the JavaFX application thread. Inside this method, we have given some data “Stage” is the keyword and “stage” is the primary stage. This method throws an Exception.

stage.setScene(scene); 
stage.setTitle("hello"); 
stage.show(); 

setScene(scene); is used because a JavaFX scene can be attached to only a single stage at a time, and the stage can also only display one scene at a time. setTitle is used to set the title for the stage, in the above case the title set is hello. show() is used to display the stage.

The output for the above code is.

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 java.lang.reflect.Method.invoke(Unknown Source) 
at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source) 
at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source) 
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) 
at sun.reflect.NativeMethodAccessorImpl.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) 
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) 
at javafx.fxml.FXMLLoader.load(Unknown Source) at application.Main.start(Main.java:14) 
at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$153(Unknown Source) at com.sun.javafx.application.LauncherImpl$$Lambda$51/1778973910.run(Unknown Source) 
at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$166(Unknown Source) 
at com.sun.javafx.application.PlatformImpl$$Lambda$44/1051754451.run(Unknown Source) 
at com.sun.javafx.application.PlatformImpl.lambda$null$164(Unknown Source) 
at com.sun.javafx.application.PlatformImpl$$Lambda$47/813155481.run(Unknown Source) 
at java.security.AccessController.doPrivileged(Native Method) 
at com.sun.javafx.application.PlatformImpl.lambda$runLater$165(Unknown Source) 
at com.sun.javafx.application.PlatformImpl$$Lambda$46/1775282465.run(Unknown Source) 
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) 
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) 
at com.sun.glass.ui.win.WinApplication.lambda$null$141(Unknown Source) 
at com.sun.glass.ui.win.WinApplication$$Lambda$37/1109371569.run(Unknown Source) ... 1 more 
Exception running application application.Main

Explanation

In the above-shown output, the error occurred at the parameter position at FXML Loader which loads the XML object. We see a NullPointerException has occurred when we are trying to open a file, the reason for this exception could be a wrong path. When using Class.getResource() method we must provide a local path from the location of the class which is called the method.

Solutions

  1. Provide a correct path where the file is present.

  2. You can fix the exception by updating your code or adding the required JavaFX modules. As a result, your code will work as expected.

  3. The exception in application start method java.lang.reflect.InvocationTargetException is an error code that occurs when you are using JavaFX. There are myriad situations that cause this error. Keep on reading, as we’ll give you detailed explanations of these causes and their fix.

  4. When you load your FXML file with FXMLLoader, observe if there is a forward slash in the pathname of the FXML file. If you are getting an exception, remove this forward slash. Afterward, your code should work. For example, the following is a code we showed you earlier:
FXMLLoader loader = new FXMLLoader(Main.class.getResource(“/File.fxml”));

From the code, we have the forward slash before File.fxml but in the code below, we’ve eliminated the forward slash. So, your code should work.

// Take out the “/” before the FXML file

FXMLLoader loader = new FXMLLoader(Main.class.getResource(“File.fxml”));

There are cases where the absence of the forward slash caused an exception. So, if you are in such a situation, add the forward slash to prevent the exception.

  1. In IntelliJ IDEA, you’ll need to add the JavaFX modules as a VM option based on your Operating System. We say “JavaFX modules” because JavaFX is a collection of modules. We base this solution on the “Getting Started” tutorial on OpenJFX. Earlier, we mentioned that if you compile your code without the JavaFX modules, you’ll get an error.
    a) To add the VM options, go to the following location in IntelliJ IDEA:
    Run → Edit Configurations
    b) At the above location, if you are on Windows, add the VM options using the following:
    –module-path “pathtojavafx-sdk-17.0.1lib” –add-modules javafx.controls,javafx.fxml
    c) If you are on Linux or Mac, use the following:
    –module-path /path/to/javafx-sdk-17.0.1/lib –add-modules javafx.controls,javafx.fxml


  2. A starting forward slash in the path name of an FXML file can cause an exception.
    a) Not setting the root before loading the XML file leads to an exception.
    b) To prevent an exception when working with JavaFX in IntelliJ, use JavaFX modules.
    c) You can add JavaFX modules as VM options in IntelliJ IDEA.
    d) You can prevent the failure from happening by setting the root on FXMLLoader before you load your FXML file.


  3. Add a SonarLint plugin to the Integrated Development Environment that helps in evaluating or handling the exceptions at the write time.

Conclusion

In the above article, we have learned about what is JavaFX, what is Exception in the application start method, the Cause Exception in the application start method, sample code to understand this exception, and the solution to this exception.

Do you want

Java Tutor?

Aniket Malik

Aniket Malik

ORACLE CERTIFIED JAVA TUTOR

🟢 Oracle Certified Java Expert

🟢 300 + ⭐ ⭐ ⭐ ⭐ ⭐

🟢 B.tech & M.tech in CSE

🟢 6+ Years of Teaching Experience

🟢 Worked as SE in Virtusa and Digidez

Do you want

Java Tutor?