Search code examples
javajavafxfxml

JavaFX Exception: Internal graphics not initialized yet


I'm trying to load a controller in the constructor, and to do so I have to call loader.load() first, otherwise getController() returns null, as I've read in multiple stackoverflow answers.

protected static final FXMLLoader connectLoader = new FXMLLoader(GuiManager.class.getResource("/scenes/connect.fxml"));

private final ConnectController connectController;

public Gui() {

    try {
        connectLoader.load();
    } catch (IOException e) {
        e.printStackTrace();
    }

    connectController = connectLoader.getController();
}

However load() throws this exception:

javafx.fxml.LoadException: 
/home/user/IdeaProjects/project/target/classes/scenes/connect.fxml:13
...
Caused by: java.lang.RuntimeException: Internal graphics not initialized yet
...

The controller is specified in the fist line of the fxml:

<AnchorPane xmlns="http://javafx.com/javafx/10.0.2-internal" xmlns:fx="http://javafx.com/fxml/1" fx:controller="com.project.gui.ConnectController">

And line 13 of the fxml file (referenced in the exception) is a quite innocuous

<Image url="@../image.png" />

Any suggestion is very welcome, thank you in advance.


Solution

  • The problem was that the start() method of Application hadn't finished executing, and the stage wasn't created yet at the time load() was called.

    Good suggestions on how to test this also here