简体   繁体   中英

Gradle with JavaFX: What am I doing wrong?

I'm relatively new (about 1 month) to JavaFX, and even newer to Gradle. My project is here, on Github . When I run ./gradlew build , it works fine. But, when I run ./gradlew run , I get all this:

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

What am I doing wrong?

Is this the base of all the problems?

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)

What would be the correct path to the .fxml files?

Thank you!

Yes, the NullPointerException is the root cause of the whole problem. You red resources incorrectly. Please replace this line (in class ro.badilos.evisu.EviSU :

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

with the following one:

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

And change the following lines in 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";

to:

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

After introducing these changes all resources will be read correctly. Running gradle clean run still results in exception but this is another one.

To fix the new exception change (in 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();
    }
}

to:

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

Remember to always refer to resources via ClassLoader . Now it runs, however another NPE is printed to console.

EDIT

Ok, the final NPE is cause by:

@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();
    }
}

Should be:

@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();
    }
}

And the last thing, you need to fix the reference to Nomeclator.db in ro.badilos.controller.InfoSituatiaProdusaController as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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