Justin
Justin

Reputation: 886

Toolkit not initialized Java FX

I am getting the follow error when I run a JavaFx application as "Run" only. Debug works fine...

Exception in thread "main" java.lang.ExceptionInInitializerError
    at Goose.Program.<clinit>(Program.java:26)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Class.java:264)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:123)
Caused by: java.lang.IllegalStateException: Toolkit not initialized
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:273)
    at com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:268)
    at com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:550)
    at com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:512)
    at javafx.scene.control.Control.<clinit>(Control.java:87)
    ... 4 more

I have read that you should subclass Application but I am already doing that so I am not sure why it doesn't work... It works fine if I debug but as soon as I try to run the application instead of debugging it, it throws that error message. Which is a little crazy.... Anyone have any idea what the heck is going on? Here is the code.

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.input.KeyCode;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class Program extends Application{
    TextField input;
    GameServer gm;
    Player p = new Player();

    /**
     * Just starts our GameServer
     */
    public static void main(String[] args) throws Exception {
        launch(args);
    }

    public static final TextArea textArea = new TextArea();

    @Override
    public void start(Stage primaryStage) {
        p.setState(Player.States.Ready);
        p.setAccess(Player.AccessStatus.GameMaster);
        input = new TextField();
        input.setPrefWidth(500);
        input.setOnKeyPressed(event -> {
            if(event.getCode().equals(KeyCode.ENTER)){
                textArea.appendText("Command: " + input.getText() + "\n");
                handleEvent(input);
                input.setText("");
            }
        });

        GridPane gridPane = new GridPane();
        gridPane.setAlignment(Pos.CENTER);
        gridPane.setHgap(10);
        gridPane.setVgap(10);
        gridPane.add(input, 0, 0, 2, 1);
        gridPane.add(textArea, 0,2, 2, 1);

        Scene scene = new Scene(gridPane, 530, 250);
        primaryStage.setMaxWidth(540);
        primaryStage.setMaxHeight(280);
        primaryStage.setMinWidth(540);
        primaryStage.setMinHeight(280);
        primaryStage.setTitle("My Server");
        primaryStage.setScene(scene);
        primaryStage.show();
        primaryStage.setOnCloseRequest(we -> {
            try {
                textArea.appendText("Shutting down server...");
                if(gm.gameworld.getRunning()) {
                    gm.gameworld.setRunning(false);
                    Thread.sleep(2000);
                }
                System.exit(1);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        });
        run();
    }

    public void run(){
        try {
            GameServer gameServer = new GameServer();
            this.gm = gameServer;
            gameServer.start();
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public void handleEvent(TextField textField){
        try {
            String eventKey = textField.getText().trim();
            Event e = gm.gameworld.getEventHandler().stringToEvent.get(eventKey);
            if(e != null) {
                e.setPlayer(p);
                e.ready(gm.gameworld);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

Upvotes: 0

Views: 5149

Answers (1)

Rg Brk
Rg Brk

Reputation: 11

There is a 'public void init() throws Exception' method in Application class whose documentation says:

"The application initialization method. This method is called immediately after the Application class is loaded and constructed. An application may override this method to perform initialization prior to the actual starting of the application". "NOTE: This method is not called on the JavaFX Application Thread. An application must not construct a Scene or a Stage in this method. An application may construct other JavaFX objects in this method."

So, I suppose that you should move: p = new Player(); and textArea = new TextArea(); to it.

Upvotes: 1

Related Questions