Fix JavaFx Exception in Application Start Method

Rashmi Patidar Feb 01, 2023 Dec 08, 2021
Fix JavaFx Exception in Application Start Method

JavaFX is a highly enriched library whose code gets written in native Java code. The library gets used to making Rich Internet Applications, often known as RIA.

The library is a set of interfaces and classes that are easily understandable and are a friendly alternative to Java Virtual Machine or JVM. The code written using the library can run across multiple platforms without fail like desktops, mobiles, televisions, etc.

Long back, the graphical User Interface gets built using Swing templates, but after the advent of JavaFX, one can easily rely on the language to work over the same. The applications built using JavaFx have a penetration rate of 76 percent.

The Exception in Application start method is the runtime error that occurs when the application is running and when compilation gets done. The state occurs when the application is inefficient in loading runtime variables or files. It can throw NullPointerException, FileNotFound type of exceptions when not handled properly.

Additionally, plugins like SonarLint, programming mistake detector(PMD), find bugs can help identify the runtime issues beforehand without actual program runs.

Below is an example to show the Exception in Application start method error in JavaFx.

import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;

public class ApplicationStart extends Application {
    public static void main(String[] args) {

    public void start(Stage stage) throws Exception {
        Parent parent = FXMLLoader.load(getClass().getResource("AnyXML.fxml"));
        Scene scene = new Scene(parent);
        stage.setTitle("First Swing Sample");;

The above source code that seems to be in JavaFx has a main method in the ApplicationStart class. The given class extends an abstract Application class and is specifically available in JavaFX Library.

It has a default theme called Caspein that gets launched once you start the application. The launch is a static method present in the Application class and gets called from the main function. It takes variable arguments or varargs as its parameters. It throws IllegalStateException if the launch method gets called more than once.

The Application class has one abstract method whose implementation must be present in ApplicationStart class. The override annotation shows that the code below the annotation belongs to the parent Application class. The implementation of the method that gets proceeded by the annotation override is present below the annotation.

The start method is the main entry for the JavaFX applications, as main is the entry location for the Java applications. The main method gets first called when the Application or main thread gets initialized.

The function takes Stage as the parameter. The Stages denotes the primary step or view and gets loaded when the application launches in the applet viewer. It also throws Exception that gets defined along with the method.

The first statement inside the method is to load the XML file. The FXMLLoader class loads an object hierarchy from the XML object model. It gets used to bring the object hierarchy from an FXML document into a Parent instance. It takes the parameter as the URL to the location where the XML document hierarchy is present.

The resultant gets stored in a Parent class instance that holds the subtypes in the graph format. The Scene class present in the JavaFX library is the container unit that stores all the data in a graph view. The background of the scene gets filled by the specified property. The instance of Stage class gets created and can get used with other properties.

Below mentioned are the properties used to display the scene over the browser.

  • The setScene method makes its use to specify the scene and gets used along with the stage instance variable.
  • The setTitle function gets used to set scene title present over the browser. The show function gets used to populate the scene over the stage.

Below is the output for the above code block.

Exception in Application start method
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(
	at com.sun.javafx.application.LauncherImpl.launchApplication(
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(
	at java.lang.reflect.Method.invoke(
	at sun.launcher.LauncherHelper$FXHelper.main(
Caused by: java.lang.RuntimeException: Exception in Application start method
	at com.sun.javafx.application.LauncherImpl.launchApplication1(
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$1(
Caused by: java.lang.NullPointerException: Location is required.
	at javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml.FXMLLoader.loadImpl(
	at javafx.fxml.FXMLLoader.load(
	at ApplicationStart.start(
	at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$8(
	at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$7(
	at com.sun.javafx.application.PlatformImpl.lambda$null$5(
	at Method)
	at com.sun.javafx.application.PlatformImpl.lambda$runLater$6(
	at Method)
	... 1 more
Exception running application ApplicationStart

In the output shown above, the issue gets raised at the parameter position that uses the FXMLLoader class to load the XML object, but it returns a null value. In simple words, the getResource() method does not locate the path provided in the function parameter.

Hence, the null value populates NullPointerException, which is a type of runtime exception. And are handled by giving an absolute path where the file can get located. The stack trace often shows the line number where the issue starts populating. The target must be correct when get given in the load parameter.

Hence the given solution to the problem is below.

  1. Give the absolute path to the location where the file gets present.
  2. Add a SonarLint plugin to the Integrated Development Environment that helps in evaluating or handling the exceptions at the write time.
Rashmi Patidar avatar Rashmi Patidar avatar

Rashmi is a professional Software Developer with hands on over varied tech stack. She has been working on Java, Springboot, Microservices, Typescript, MySQL, Graphql and more. She loves to spread knowledge via her writings. She is keen taking up new things and adopt in her career.


Related Article - Java Error