Search code examples
eclipsenetbeansjavafxfxmlefxclipse

JavaFX show stage


I imported a JavaFX project from NetBeans to Eclipse. Strangely I am unable to execute the code that worked just fine in NetBeans. I have a little gui set up with SceneBuilder. A little .fxml I just want to show up - there's no functional code at all at this point. My main class looks like this:

public class Main extends Application {

public void start(Stage stage) throws Exception {
        Parent root = FXMLLoader.load(getClass().getResource("FXMLDocument.fxml"));
        Scene scene = new Scene(root);

        stage.setScene(scene);
        stage.setTitle(Strings.appName);
        stage.getIcons().add(new Image("sql.png")); 
        stage.show();
    }

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

But when I try to execute the code the eclipse compiler returns:

Exception in Application start method java.lang.reflect.InvocationTargetException at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source) at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source) at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source) at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source) at java.lang.reflect.Method.invoke(Unknown Source) at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source) Caused by: java.lang.RuntimeException: Exception in Application start method at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$156(Unknown Source) at java.lang.Thread.run(Unknown Source) Caused by: java.lang.NullPointerException: Location is required. at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.loadImpl(Unknown Source) at javafx.fxml.FXMLLoader.load(Unknown Source) at core.Main.start(Main.java:27) at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$163(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$176(Unknown Source) at com.sun.javafx.application.PlatformImpl.lambda$null$174(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at com.sun.javafx.application.PlatformImpl.lambda$runLater$175(Unknown Source) at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source) at com.sun.glass.ui.win.WinApplication._runLoop(Native Method) at com.sun.glass.ui.win.WinApplication.lambda$null$149(Unknown Source) ... 1 more Exception running application core.Main

This is my project structure:

SQL
│   .classpath
│   .project
│
├───.settings
│       org.eclipse.jdt.core.prefs
│
├───bin
│   ├───gui
│   │       FXMLDocument.fxml
│   │       FXMLDocumentController.class
│   │       Main.class
│   │       sql.png
│   │
│   ├───print
│   │       Allgemein.class
│   │       Mahnung.class
│   │       PDF.class
│   │       Rechnung.class
│   │
│   └───various
│           Strings.class
│
└───src
    └───gui
            FXMLDocument.fxml
            FXMLDocumentController.java
            Main.java

What am I doing wrong here? Or is eclipse expecting something here, that NetBeans wasn't?!?


Solution

  • Like suggested by @James_D I was able to track down the problem using

    System.out.println(getClass().getResource("Main.class"));
    

    and

    System.out.println(getClass().getResource("FXMLDocument.fxml"));
    

    Turned out my classpath must somehow have gotten corrupted while copying the project from my ubuntu laptop running NetBeans to my main Windows machine running 10 and Eclipse. I fixed it and everything works fine now.