Sunday, September 14, 2014

Exception in Application start method java.lang.reflect.InvocationTargetException

Following the stack trace of the above error, I found the following:

Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3223)


This due to the line:

GridPane root = (GridPane) FXMLLoader.load(getClass().getResource("ChatterServer.fxml"));

but the file "ChatterServer.fxml" wasn't at the expected location, which is where the application.class files are.

I'm using the eclipse environment, so this is the /bin folder. Moving a copy of the fxml file to this location fixed this issue.

Skeleton class

Here is a basic class that uses JavaFx:


package Chatter;

import javafx.application.Application;
import javafx.stage.Stage;

public class Delete extends Application
{
    @Override
    public void start(Stage primaryStage) throws Exception
    {
        // Do stuff
       
    }
   
    public static void main(String[] args)
    {
        launch();

    }

}

Things to note:

1. launch() calls the start() method, which is needed for Applications
2. The Stage is the visual window upon which content is added. At the moment, you can't see anything because nothing has been added.

JavaFX

This is the latest in GUI development in the Java SDK. Introduced as a separate tool with Java7, it has now included with Java8.

I'll go through some basic setups and move on to more complex developments using a chat like application for examples.

I will call the application JChatter (as in Java chatter), for example purposes.