简体   繁体   English

应用程序启动方法异常-JavaFX

[英]Application Start Method exception - JavaFX

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... 我想说的是,我在SO中看了很多东西,但没有帖子对我有帮助,所以我再次询问...

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. 到目前为止,我读过的所有内容都说:“这就是FXML找不到控制器的原因!”,所以我检查了一下,这真的没问题。 Here is the controler statement: 这是控制语句:

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

I think this information can be requested: 我认为可以要求提供以下信息:

There is a couple ways to do this. 有两种方法可以做到这一点。 I prefer method 1, since it seems more straightforward to me. 我更喜欢方法1,因为它对我来说似乎更简单。

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/ . 例如,如果我有一个包含MainController.java文件的java/hypnic/jerk/controllers/层次结构,并且我想加载一个将XML文件用作其控制器的FXML文件,则将其放在resources/hypnic/jerk/controllers/ Then, when I call FXMLLoader.load(); 然后,当我调用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. 根据我的理解和看法,这告诉加载程序使用MainController设置的路径,并从那里的路径中获取mainScreen.fxml文件。

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. 在我看来,这是最简单的方法,因为它会强制您保持命名约定,并且您知道每个FXML文件在哪个位置用于关联的控制器文件。

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 您还可以使用路径找到它,在您的情况下,您的loginScene.fxmlfxml/文件夹中,因此从Main我会做

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. 是该名称下的新文件夹,您需要添加../直到返回到java文件夹。

Lets use your example with a path of java/br/com/one/two/three/main/ . 让我们将示例与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. 由于您的代码说的是getClass().getResource("fxml/loginScene.fxml")您要在resources/br/com/one/two/three/fxml/文件夹中resources/br/com/one/two/three/fxml/文件。 The path is based off the calling class Main.java , so it can't find it because the path in resources doesn't exist. 该路径基于调用类Main.java ,因此找不到该路径,因为resources中的路径不存在。

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. 因此,在这种情况下,如果我正确地进行了数学计算,则需要5 ../才能找到fxml/loginScene.fxml文件。 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. 希望这会有所帮助。

In fact, you're getting a NPE. 实际上,您正在获得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();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM