0

When I run the main class, in a Maven project, I get:

Exception in Application start method

From what I've understood i'ts because the FXML file won't load.

This is the Filesystem Hierarchy

 src
    └- main
        ├- java
        |   └- zenit
        |      └- ui
        |         └- TestUI.java
        └- resources
            └- zenit
               └- ui
                  └- Main.fxml

In Eclipse (inside src)

enter image description here

enter image description here

Reading apache's maven guide and other statck questions, following code-snippet seems to be the right way to create the FXMLLoader and point to the Main.fxml file.

public class TestUI extends Application {
    public void start(Stage stage) throws IOException {

        FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));

        .
        .   // set controller, load parent, set scene etc.
        .

    }

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

But this code will throw the previously stated exception.

When I tried to pinpoint where in my code the Exception came from I figured out it was form the FXMLLoader. So I ran the different sysouts (one at a time), on the line above FXMLLoader loader = new FXMLLoader(..) :

    System.out.println(getClass().getResource("Main.fxml"));
    System.out.println(getClass().getResource("main/resources/zenit/ui/Main.fxml"));
    System.out.println(getClass().getResource("../../../resources/zenit/ui/Main.fxml"));

For all, the console output was: (obviously not expected output)

null

So, my question are,

Why does .getResource("Main.fxml") return null here?

How do I correctly access the Main.fxml file in this case?

My system:

  • Maven: Apache Maven 3.6.0
  • Java SE: 11.0.2
  • JavaFX: 11.0.2
  • Eclipse: 2018-12 (4.10.0)
  • OS: macOS Mojave - v10.14.3
Strazan
  • 147
  • 1
  • 16
  • try giving the package names in front of the "Main.fxml" in getResourse() method because the class TestUI and the fxml file are in different locations – Dilini Peiris Apr 11 '19 at 10:26
  • I've updated my question.. Same thing.. :( – Strazan Apr 11 '19 at 10:29
  • `package zenit.ui; ... class TestUI {}`. In eclipse the source directories must be java and resources for maven compliance. Look into the jar for the file `/zenit/ui/Main.fxml` – Joop Eggen Apr 11 '19 at 10:30
  • @Strazan try putting "../../../rescources/zenit/ui/Main.fxml" and see? – Dilini Peiris Apr 11 '19 at 10:32
  • @JoopEggen I have java and resources no? Should I remove src and only have those? I found the `Main.fxml`-file in the jar" – Strazan Apr 11 '19 at 10:34
  • @DiliniPeiris same thing :/ – Strazan Apr 11 '19 at 10:36
  • @Strazan i might be saying a stupid thing.. but the spellings are wrong for "resources". both you and i have written "rescources" previously. correct that and see? because i once had this problem long time ago and it was a matter of correctly writing the package names – Dilini Peiris Apr 11 '19 at 10:39
  • @DiliniPeiris haha jesus christ :P well unfortunately, still `null` – Strazan Apr 11 '19 at 10:42
  • You may have other problems - typically when it cannot find the fxml - the exception will say "location not set" – Edv Beq Apr 11 '19 at 10:47
  • For maven you have the correct hierarchy. If you open the jar with 7zip or whatever you should see `/zenit/ui/Main.fxml`. In fact you might also use an absolute path `getResource("/zenit/ui/Main.fxml")`. (It _just_ might be that somehow the maven project build is not configured correctly in eclipse.) Check the jar – Joop Eggen Apr 11 '19 at 11:00
  • @JoopEggen I do see the `Main.fxml` inside the jar under zenit/ui/ !! and when I use `getResource("/zenit/ui/Main.fxml")` sysout will print the path, and not `null`!! cheers! but I still get the exception tho :/ – Strazan Apr 11 '19 at 11:43
  • @EdvBeq any idea what the issue might be? :D – Strazan Apr 11 '19 at 11:43
  • If you upload your project somewhere - I can take a look tonight. – Edv Beq Apr 11 '19 at 12:43
  • @EdvBeq Thank you, that's very kind of you! Here is the project, ( feature/maven2 is the branch. I'm trying to convert the project to maven. ) https://github.com/strazan/zenit/tree/feature/maven2 – Strazan Apr 11 '19 at 13:02
  • The Main.fxml evidently is found (the URL is not null). So the exception (which you did not list) must point to some (next) problem. Start with a simple fxml. – Joop Eggen Apr 11 '19 at 13:10
  • @JoopEggen alright, I don't get any stack traces. I wrote everything I got!! I tried with a new fxml now (a `pane` wrapped in an `anchorpane`) same thing :/ – Strazan Apr 11 '19 at 13:41
  • Hopeless, but try a sample Mave JavaFX application. – Joop Eggen Apr 11 '19 at 14:17
  • @JoopEggen yee!! cheers – Strazan Apr 11 '19 at 18:27

1 Answers1

1
  1. I think your main problem is the library "ikonli." If you try to open the fxml with scenebuilder you will see the exception. My recommendation is that you lose that dependency. Use something simpler such as svg graphics:

    • In the fxml button > add a region > set the region size > then in the style section add the css property -fx-shape > and in the value set the svg path in quotes i.e.: "M827.6 ... 122z"

    • Next add another css property -fx-scale-shape > and set its value to true

The above does not require any third party libraries and you can find all the icons for free online.

  1. I recommend that you load your fxml from the main controller class. In the fxml check the property fxroot and add a constructor to your main controller like so

public MainController() {

try {
    FXMLLoader loader = new FXMLLoader(getClass().getResource("Main.fxml"));
    loader.setController(this);
    loader.setRoot(this);
    loader.load();

} catch (IOException e) {
    e.printStackTrace();
}

}

  1. Your controller should extend the root container like so:

    public class MainController extends VBox {

then in the main class just pass the main controller to the scene

Scene scene = new Scene(controller);
  1. I believe your maven project was not set up correctly. You should right click on your project > configure > convert to maven project. Then clean up the packages if they turn red.

  2. Last thing - you need to add another class called Launcher - and then you start your application from it - do not remove the main method from your main class

package zenit.ui;

public class Launcher {

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

}

Hope this helps!

Edv Beq
  • 910
  • 3
  • 18
  • 43
  • aaah!!! thank you, removing ikonli helped! Weird tho, since ikonli did work when the project was a regular java project.. any idea why? but, if I set root(this) I get `Root value already specified.` what should I have/not have in the fxml? – Strazan Apr 12 '19 at 04:40
  • if I remove the `VBox` from the fxml I get `Invalid root element.` – Strazan Apr 12 '19 at 04:43
  • I wrapped the fxml code in `` , and now, I get no Exceptions, the application is running, but nothing visual appear :/ – Strazan Apr 12 '19 at 05:01
  • sorry for spamming, it works now!! thank you so much you are a legend! <3 – Strazan Apr 12 '19 at 05:07