0

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();
        }
    }
}
Justin
  • 866
  • 1
  • 13
  • 29
  • What is in the constructor for `Player`? – James_D Sep 02 '16 at 23:23
  • Nothing, blank constructor. Also removed All object references except JavaFx references and still same issue.... :(. But that debug works perfectly lol...... Such a weird issue. – Justin Sep 02 '16 at 23:27
  • Looks like it has something to do with the public static final TextArea textArea = new TextArea(); Are you not allowed to have a static final variable in the class? I removed the static and it works fine....... Bah..... What on earth? – Justin Sep 02 '16 at 23:33
  • @James_D is there something that I do not know about with javafx? lol. Not sure what I am missing and why I cant have a static. – Justin Sep 02 '16 at 23:35
  • 1
    Sorry: hit enter before I meant to: entire comment: Oh yeah, that makes sense. That will attempt to create the text field before launch() is invoked. `launch()` starts the FX toolkit - so you're trying to create a FX control before the FX toolkit starts. – James_D Sep 02 '16 at 23:36
  • You can have a static control (if you really want: why though??), just not in the application class (even more: why?). – James_D Sep 02 '16 at 23:37
  • True, I just had it there because it was a copy and paste. And it didn't break anything in debug :P. So thanks for explaining! If you post the answer I will accept it :). Thanks! – Justin Sep 02 '16 at 23:47
  • I'll convert to an answer when I am back at my computer. Glad it helped. – James_D Sep 02 '16 at 23:51

1 Answers1

1

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.

Dov Benyomin Sohacheski
  • 7,133
  • 7
  • 38
  • 64
Rg Brk
  • 11
  • 5