0

Today I have something, which should be a common task, but somehow I am getting Exceptions for it. I have the following code:

public class ExampleScene extends Scene {

   public ExampleScene(Parent parent) {     
       super(parent,Variables.width,Variables.height);
   }

   public static Parent fill() {        
       HBox gp = new HBox();
       gp.getChildren().add(new Label("hi"));
       return gp;
   }
}

I'm calling them like this:

Parent par = ExampleScene.fill();
ExampleScene example = new ExampleScene(par); 

But this produces the following exception:

Exception in thread "main" java.lang.ExceptionInInitializerError
    at de.rrcomtech.basicgui.test.ExampleScene.fill(ExampleScene.java:20)
    at de.rrcomtech.basicgui.test.startASimpleWindow.main(startASimpleWindow.java:18)

Caused by: java.lang.IllegalStateException: Toolkit not initialized
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:396)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.runLater(PlatformImpl.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.setPlatformUserAgentStylesheet(PlatformImpl.java:673)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.setDefaultPlatformUserAgentStylesheet(PlatformImpl.java:635)
    at javafx.controls/javafx.scene.control.Control.<clinit>(Control.java:99)
    ... 2 more

The marked line is

gp.getChildren().add(new Label("hi"));

I cannot understand where is the fault in my code.

Blitzkoder
  • 1,768
  • 3
  • 15
  • 30
IPodFan
  • 91
  • 1
  • 9
  • JavaFX is not initialised: See: https://stackoverflow.com/questions/14025718/javafx-toolkit-not-initialized-when-trying-to-play-an-mp3-file-through-mediap/38883432 – StuPointerException May 31 '18 at 20:25
  • 1
    It looks like you are calling this before the JavaFX toolkit has been started (in particular, you seem to be calling this in a `main()` method). You need to call this in the proper place in the FX Application lifecycle. See, perhaps, https://docs.oracle.com/javase/10/docs/api/javafx/application/Application.html – James_D May 31 '18 at 20:25

0 Answers0