-1

How I load it:

public class App extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        FXMLLoader loader = new FXMLLoader();
        Parent root = loader.load(new File("src/dreambot/guis/XPTracker.fxml").toURI().toURL());
        Controller l = (Controller) loader.getControllerFactory();
        primaryStage.setTitle("XP Tracker");
        primaryStage.setScene(new Scene(root));
        primaryStage.show();
        l.setAtkXph(23233);
    }

    public void build(String[] args) {
        launch(args);
    }
}

Controller

public class Controller {
    @FXML
    Label atkXph;

    public void setAtkXph(int num) {
        atkXph.setText(num + "/h");
    }
}

Main(String[] args) method

public class Tests {
    public static void main(String[] args) {
        new App().build(args);
    }
}

The label in my fxml:

<Label id="atkXph" fx:id="atkXph" layoutX="68.0" layoutY="71.0" text="0/h" />

My error:

Exception in Application start method
Exception in thread "main" java.lang.RuntimeException: Exception in Application start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$159(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException
    at dreambot.main.App.start(App.java:23)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$166(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$179(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$177(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$178(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
    ... 1 more
fabian
  • 80,457
  • 12
  • 86
  • 114
Jaquarh
  • 6,493
  • 7
  • 34
  • 86
  • 1
    Possible duplicate of [What is a NullPointerException, and how do I fix it?](https://stackoverflow.com/questions/218384/what-is-a-nullpointerexception-and-how-do-i-fix-it) – Zephyr Sep 19 '19 at 11:47
  • 2
    hmm ... might be unrelated, but casting ControllerFactory to Controller looks fishy .. – kleopatra Sep 19 '19 at 12:31
  • Look at the key code from https://stackoverflow.com/questions/50774910/gethostservices-showdocument-in-a-fxml-file/50775157#50775157. – SedJ601 Sep 19 '19 at 13:40
  • Possible duplicate of [getHostServices().showDocument() in a FXML File](https://stackoverflow.com/questions/50774910/gethostservices-showdocument-in-a-fxml-file) – SedJ601 Sep 19 '19 at 13:41

1 Answers1

1

Controller and controller factory are 2 different things:

The controller is the object that contains methods used with #methodName attribute values and fields that the loader injects objects to based on fx:ids.

The controller factory is used to create a controller, if you assign one to it.

controllerFactory.call(Class.forName(<fx:controller attribute value>))

is used by FXMLLoader to create an instance of the controller class, if you assign a Callback before loading the fxml.

Furthermore you use one of the static load method. This way a second FXMLLoader instance you cannot access is created and used to load the fxml. You need to specify the URL as location before loading the fxml.

FXMLLoader loader = new FXMLLoader(new File("src/dreambot/guis/XPTracker.fxml").toURI().toURL()); // maybe replace with resource access (getClass().getResource("/dreambot/guis/XPTracker.fxml"))?
Parent root = loader.load();
Controller l = loader.getController();

Also you cannot create the instance of the Application class yourself and use the standard way of launching. Application.launch always creates a new instance of the application class. In this ase making the build method static would serve the same purpose, but you can also specify the application class using the overloaded version of launch:

Application.launch(App.class, args);
fabian
  • 80,457
  • 12
  • 86
  • 114