gpt4 book ai didi

java - 使用 JavaFX Gradle : What am I doing wrong?

转载 作者:行者123 更新时间:2023-11-30 10:54:37 25 4
gpt4 key购买 nike

我是 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 文件的正确路径是什么?

谢谢!

最佳答案

是的,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();
}
}

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

关于java - 使用 JavaFX Gradle : What am I doing wrong?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33593225/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com