0

I know this question is asked for a 1000 times, but non of the possible answers is the solution for me. I try to build a Spring Bootapp that uses javaFX. Somehow he can't find my sample.fxml which is located in resources/fxml/. I already tried to clean my maven project and rebuild it, my sample.fxml should work(I started out with a normal javaFX project and everything was working), I also think my file is located at the rightplace... Any idea's?

My Main.class:

@SpringBootApplication
@EnableJpaRepositories
public class Main extends Application {

   private ConfigurableApplicationContext springContext;
   private Parent root;

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

   }

   @Override
   public void init() throws Exception {
        springContext = SpringApplication.run(Main.class);
        FXMLLoader fxmlLoader = new FXMLLoader();
        fxmlLoader.setLocation(getClass().getResource("fxml/sample.fxml"));
        fxmlLoader.setControllerFactory(springContext::getBean);
        root = fxmlLoader.load();
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("KOKW-AdminApp");
        primaryStage.setScene(new Scene(root, 800, 600));
        primaryStage.show();
        BookRepo repo = springContext.getBean("bookRepo", BookRepo.class);
        MemberRepo member = springContext.getBean("memberRepo",MemberRepo.class);
    }

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

My Stacktrace:

Exception in Application init method
java.lang.reflect.InvocationTargetException
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(LauncherImpl.java:389)
    at com.sun.javafx.application.LauncherImpl.launchApplication(LauncherImpl.java:328)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:498)
    at sun.launcher.LauncherHelper$FXHelper.main(LauncherHelper.java:767)
Caused by: java.lang.RuntimeException: Exception in Application init method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:912)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.IllegalStateException: Location is not set.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2434)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2409)
    at be.kokw.Main.init(Main.java:35)
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:841)
... 2 more
Exception running application be.kokw.Main
2
  • 1
    Is Main in the default package? (If so, that is not recommended.) If not, then the FXMLLoader will look for the fxml file relative to the class. You probably need getClass().getResource("/fxml/sample.fxml").
    – James_D
    Oct 22, 2017 at 21:47
  • No main isn't in the defaultpackage, your answer is still correct, although I figured out myself 2 days ago, still thank you for the reply
    – Daniel D.
    Oct 24, 2017 at 17:23

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.