2

I would like to say that I have looked a lot in the SO and none of the posts helped me, so I am asking again...

I'm trying to make a login screen, but I still get the same exception. My directory structure looks like this: check here

My code looks like this:

    public class main extends Application {

        @Override
        public void start(Stage stage) throws Exception {
            Parent root = FXMLLoader.load(getClass().getResource("fxml/loginScene.fxml"));
          //Parent root = FXMLLoader.load(getClass().getResource("../login/login.fxml"));
          //Parent root = FXMLLoader.load(getClass().getResource("login.fxml"));
          //Parent root = FXMLLoader.load(getClass().getResource("src/relativepath//login/login.fxml"));
          //Parent root = FXMLLoader.load(getClass().getResource("/home/somepath/src/relativepath/login/login.fxml"));
          //Parent root = FXMLLoader.load(getClass().getResource("fxml/loginScene.fxml"));

            stage.setScene(new Scene(root));
            stage.show();
        }

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

This is the exception I get:

Exception in Application start 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 start method
    at com.sun.javafx.application.LauncherImpl.launchApplication1(LauncherImpl.java:917)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$154(LauncherImpl.java:182)
    at java.lang.Thread.run(Thread.java:748)
Caused by: java.lang.NullPointerException: Location is required.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3207)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3175)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3148)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3124)
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:3104)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:3097)
    at br.com.fulltime.fullcam.mosaicoDesktop.main.Main.start(Main.java:12)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$161(LauncherImpl.java:863)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$174(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl.lambda$null$172(PlatformImpl.java:295)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$173(PlatformImpl.java:294)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.gtk.GtkApplication._runLoop(Native Method)
    at com.sun.glass.ui.gtk.GtkApplication.lambda$null$48(GtkApplication.java:139)
    ... 1 more

Everything I've read so far has said "That's why FXML can not find the controller!", So I checked and it's really all right. Here is the controler statement:

fx:controller="br.com.mycompany.myproduct.module.login.controllerLogin">

I think this information can be requested:

Noodle
  • 23
  • 4
  • 1
    "Location is not set" means that Java cannot find your FXML file. Check the path again. – Zephyr Nov 05 '18 at 18:50
  • I did not understand the downvote, I already said that nothing helped me and I researched so much ... – Noodle Nov 05 '18 at 18:50
  • Try just `/base_package/login/login.fxml`. Pass a full path to your loader. – Zephyr Nov 05 '18 at 18:53
  • I tried all those shapes and it still did not work. Even if I put the entire path of the file, it does not work. The curious thing is that I put it pretty much the same way into a test project and it worked ... – Noodle Nov 05 '18 at 18:54
  • Show us your entire project structure then, because I guarantee the problem is with that path. – Zephyr Nov 05 '18 at 18:55
  • @Zephyr, I've updated the link of the image and put the line "Parent root ..." as it is now. The IDE can access the image using the path, but I still have the same exception. – Noodle Nov 05 '18 at 19:07
  • There's the problem. Your FXML is in a resources package. I am not familiar with that structure, but I know you can't access it the same way you're trying to. – Zephyr Nov 05 '18 at 19:07
  • So I put it in the "login" package and nothing has changed :/ – Noodle Nov 05 '18 at 19:13
  • If you moved it to the `login` package, it looks like `/br/com/the/stuf/you/got/marked/out/login/controllerLogin.fxml` should work. – SedJ601 Nov 05 '18 at 19:28
  • @Sedrick I've tried this, but not work :/ (Should be the view, not controller!) – Noodle Nov 05 '18 at 19:30
  • Check out the `Github` link from [this](https://stackoverflow.com/questions/32342864/applying-mvc-with-javafx) answer. Look closely at the project structure and `ContactApp.java`. – SedJ601 Nov 05 '18 at 19:31
  • @Sedrick This was one of the answers I consulted, I understood well but I still do not know why I'm in trouble ... – Noodle Nov 05 '18 at 19:34
  • In your image, you have the FXML file under `src/main/resources/fxml`. You should use `getClass().getResource("/fxml/sceneLogin.fxml")`. – Slaw Nov 05 '18 at 19:37
  • You didn't follow the example well. I don't see where he has a resource folder in the example. I also see that he has all the `FXML` in the same package with its `Controller`. So somewhere you went astray. – SedJ601 Nov 05 '18 at 19:37

2 Answers2

0

There is a couple ways to do this. I prefer method 1, since it seems more straightforward to me.

To load a resource you need to know the path. The controllers know their own path, so I use them.

For instance, if I have a heirarchy of java/hypnic/jerk/controllers/ with a MainController.java file in it, and I want to load an FXML file that will use this java file as its controller, I put it in resources/hypnic/jerk/controllers/. Then, when I call FXMLLoader.load(); I do it this way:

FXMLLoader.load(MainController.class.getResource("mainScreen.fxml"));

From what I understand and how I see it, this tells the loader to use the path set forth by MainController and grab the mainScreen.fxml file from the path there.

This is, in my opinion, the easiest way to do it since it forces you to keep naming conventions AND you know where each FXML file is for the associated controller file.

You can also use pathing to find it, in your case, your loginScene.fxml is in the fxml/ folder, so from Main I would do

FXMLLoader.load(getClass().getResource("../../../../fxml/loginScene.fxml"));

The ../ means, go up one directory/folder. So depending on how may items you have blacked out, remember each . is a new folder in that name, you need to add a ../ until you are back to the java folder.

Lets use your example with a path of java/br/com/one/two/three/main/. Since your code is saying getClass().getResource("fxml/loginScene.fxml") you are looking for that file in the resources/br/com/one/two/three/fxml/ folder. The path is based off the calling class Main.java, so it can't find it because the path in resources doesn't exist.

So you need to back out of those dirs by using ../ until you get to it. each ../ represents another tier up. So in this case, if I did my math correctly, you would need 5 ../ in order to find the fxml/loginScene.fxml file. So the end result would be

FXMLLoader.load(getClass().getResource("../../../../../fxml/loginScene.fxml"));

Now remember this is just an explanation and you will have to adapt it on your own.

Hope this helped.

Hypnic Jerk
  • 1,192
  • 3
  • 14
  • 32
0

In fact, you're getting a NPE. Try to debug, then, you can try this:

public void start(Stage stage) throws Exception {
    FXMLLoader loader = new FXMLLoader(this.getClass().getResource("/fxml/loginScene.fxml"));
    Parent root = loader.load();
    stage.setScene(new Scene(root));
    stage.show();
}
FearX
  • 317
  • 3
  • 16