1

I have basic class for loading FXML files and controllers. When I try to run test, it throws ExceptionInInitializerError: null. If I comment loader.setControllerFactory(context::getBean);, there is no error. Does any one knows what's wrong? THANKS FOR HELP!

class for loading FXML files and controllers:

@Component
public class FXMLreader {
private final ApplicationContext context;
private final ResourceBundle resourceBundle;

@Autowired
public FXMLreader(ApplicationContext context, ResourceBundle resourceBundle) {
    this.context = context;
    this.resourceBundle = resourceBundle;

}

public Parent load(String fxmlPath) throws IOException{

    FXMLLoader loader = new FXMLLoader();      
    loader.setLocation(getClass().getResource(fxmlPath));       
    loader.setControllerFactory(context::getBean);
    loader.setResources(resourceBundle);

    Parent parent = loader.load();

    return parent;
}
}

Test class:

@RunWith(SpringRunner.class)
@SpringBootTest

public class FXMLreaderTest {

   @Autowired
   FXMLreader fXMLreader;

   public FXMLreaderTest() {
   }


@Test
public void test(){
    try {
        Parent parent  = fXMLreader.load("/fxml/Login.fxml");
        assertNotEquals(null, parent);
    } catch (IOException ex) {
          Logger.getLogger(FXMLreaderTest.class.getName()).log(Level.SEVERE, null, ex);
    }
}

}

Main class

@SpringBootApplication
@EnableAspectJAutoProxy
public class MainAppClass extends Application{
private static final Logger LOG = Logger.getLogger(MainAppClass.class);

protected ConfigurableApplicationContext springContext;

@Override
public void init() throws Exception{
    springContext = SpringApplicationContext();
}

@Override
public void start(Stage primaryStage) throws Exception {

}

@Override
public void stop() throws Exception{
    springContext.close();
}

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

private ConfigurableApplicationContext SpringApplicationContext() {
    SpringApplicationBuilder builder = new SpringApplicationBuilder(MainAppClass.class);
    String[] args = getParameters().getRaw().stream().toArray(String[]::new);
    builder.profiles("dev");
    return builder.run(args);
}

And error

java.lang.ExceptionInInitializerError: null
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)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at java.lang.Class.newInstance(Class.java:442)
at sun.reflect.misc.ReflectUtil.newInstance(ReflectUtil.java:51)
at javafx.fxml.FXMLLoader$InstanceDeclarationElement.constructValue(FXMLLoader.java:1009)
at javafx.fxml.FXMLLoader$ValueElement.processStartElement(FXMLLoader.java:746)
at javafx.fxml.FXMLLoader.processStartElement(FXMLLoader.java:2707)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2527)
at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2441)
at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
at com.myapp.view.FXMLreader.load(FXMLreader.java:43)
at com.myapp.view.FXMLreaderTest.test(FXMLreaderTest.java:63)
nihai1234
  • 23
  • 1
  • 4
  • Looks like the exception occurs when it tries to create one of the controls defined in your FXML file. You probably need a [MCVE] here. You should at least post the FXML. – James_D Dec 17 '17 at 16:54

0 Answers0