Gradle with JavaFX:我做错了什么?

时间:2015-11-08 11:04:31

标签: java gradle javafx

我对JavaFX相对较新(大约1个月),甚至比Gradle更新。我的项目是here, on Github。当我运行./gradlew build时,它运行正常。但是,当我运行./gradlew run时,我得到了所有这些:

Alins-MacBook-Pro:evisu Alin$ ./gradlew run
:compileJava UP-TO-DATE
:processResources UP-TO-DATE
:cssToBin SKIPPED
:classes UP-TO-DATE
:run
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:497)
    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:497)
    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$152(LauncherImpl.java:182)
    at com.sun.javafx.application.LauncherImpl$$Lambda$51/1323468230.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Caused by: java.lang.NullPointerException: inputStream is null.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
    at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43)
    at ro.badilos.evisu.EviSU.start(EviSU.java:27)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$54/2140593657.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$47/186276003.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/1714838540.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/237061348.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
Exception running application ro.badilos.evisu.EviSU
:run FAILED

FAILURE: Build failed with an exception.

* What went wrong:
Execution failed for task ':run'.
> Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_51.jdk/Contents/Home/bin/java'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.

BUILD FAILED

Total time: 3.74 secs

我做错了什么?

这是所有问题的基础吗?

Caused by: java.lang.NullPointerException: inputStream is null.
    at javafx.fxml.FXMLLoader.loadImpl(FXMLLoader.java:2459)
    at javafx.fxml.FXMLLoader.load(FXMLLoader.java:2429)
    at ro.badilos.evisu.EviSU.loadMainPane(EviSU.java:43)
    at ro.badilos.evisu.EviSU.start(EviSU.java:27)

.fxml文件的正确路径是什么?

谢谢!

1 个答案:

答案 0 :(得分:1)

是的,NullPointerException是整个问题的根本原因。你的资源不正确。请替换此行(在课程ro.badilos.evisu.EviSU中:

Pane mainPane = (Pane) loader.load(getClass().getResourceAsStream(VistaNavigator.MAIN));

使用以下内容:

Pane mainPane = (Pane) loader.load(getClass().getClassLoader().getResourceAsStream(VistaNavigator.MAIN));

并更改ro.badilos.evisu.VistaNavigator中的以下行:

public static final String MAIN    = "src/main/resources/Main.fxml";
public static final String VISTA_1 = "src/main/resources/NewSU.fxml";
public static final String VISTA_2 = "src/main/resources/NewSMURD.fxml";

为:

public static final String MAIN    = "Main.fxml";
public static final String VISTA_1 = "NewSU.fxml";
public static final String VISTA_2 = "NewSMURD.fxml";

引入这些更改后,将正确读取所有资源。运行gradle clean run仍会导致异常,但这是另一个。

修复异常更改(在ro.badilos.evisu.VistaNavigator中):

public static void loadVista(String fxml) {
    try {
        mainController.setVista(
            (Node) FXMLLoader.load(
                VistaNavigator.class.getResource(
                    fxml
                )
            ));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

到:

public static void loadVista(String fxml) {
    try {
        mainController.setVista(
            (Node) FXMLLoader.load(
                VistaNavigator.class.getClassLoader().getResource(
                    fxml
                )
            ));
    } catch (IOException e) {
        e.printStackTrace();
    }
}

请务必始终通过ClassLoader引用资源。现在它运行,但另一个NPE打印到控制台。

修改

好的,最终的NPE原因是:

@Override
public void initialize(URL location, ResourceBundle resources) {
    try {
        Image focIcon = new Image(getClass().getResourceAsStream("pool/foc16px.png"));
        btnNewSU.setGraphic(new ImageView(focIcon));

        Image smurdIcon = new Image(getClass().getResourceAsStream("pool/smurd16px.png"));
        btnNewSMURD.setGraphic(new ImageView(smurdIcon));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

应该是:

@Override
public void initialize(URL location, ResourceBundle resources) {
    try {
        Image focIcon = new Image(getClass().getClassLoader().getResourceAsStream("foc16px.png"));
        btnNewSU.setGraphic(new ImageView(focIcon));

        Image smurdIcon = new Image(getClass().getClassLoader().getResourceAsStream("smurd16px.png"));
        btnNewSMURD.setGraphic(new ImageView(smurdIcon));
    } catch (Exception e) {
        e.printStackTrace();
    }
}

最后,你需要修复Nomeclator.db中对ro.badilos.controller.InfoSituatiaProdusaController的引用。