1

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?!?

farosch
  • 210
  • 4
  • 16
  • The error message says it can't find the FXML file. Presumably you copied the FXML file into the source folder; make sure Eclipse is deploying it along with the class file. It might help to show your project layout. – James_D Oct 19 '15 at 12:59
  • It seem so, but I can't really explain why. My gui package is containing both the Main.java and the FXMLDocument.fxml – farosch Oct 19 '15 at 13:22
  • Is the fxml file copied to the same directory as the Main.class file? That's where it's going to be loaded from at runtime. – James_D Oct 19 '15 at 13:24
  • Yes, they're both in the same directory. As I already said, in NetBeans this works like a charm. But I wanted to export the project to Eclipse since I like this IDE more. No clue why the eclipse compiler doesn't find the .fxml :/ – farosch Oct 19 '15 at 13:34
  • When you run the application from Eclipse, it will compile the .java file to some location (configurable, but probably a folder called `bin` which is in the same parent folder as `src`), and then run from there. NetBeans likely has a different setup. So if you copy the class file from NetBeans you probably have two class files. You need to make sure the FXML file is in the same folder as the class file that Eclipse generates when it compiles (so make sure Eclipse is deploying the FXML file). [Edit] your question to include your project structure, including the output folder (i.e. bin). – James_D Oct 19 '15 at 13:40
  • And just to note: it's not the *compiler* that can't find the FXML file; FXML is not compiled. It can't be found at *run time*. Again, to emphasize, it's not enough for the FXML to be in the same place as the *.java* file, it must be in the same place as the *.class* file. – James_D Oct 19 '15 at 13:52
  • Ah ok, now I got you. I updated my post, but I don't think this is a directory problem. – farosch Oct 19 '15 at 14:18
  • So try this, right before you try to load the FXML: `System.out.println(getClass().getResource("Main.class");` and `System.out.println(getClass().getResource("FXMLDocument.fxml");`. I'd comment out setting the icon, too, as that definitely looks wrong. – James_D Oct 19 '15 at 14:24
  • ok so I was able to track down the problem. It seemed something was messed up in my classpath. I was able to fix the Problem and now everything works ;) Thanks a lot – farosch Oct 19 '15 at 16:40

1 Answers1

0

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.

farosch
  • 210
  • 4
  • 16