使用gradle构建项目时的图像产品nullpointer - Java

时间:2016-12-03 13:40:37

标签: java gradle javafx

参见TL的底部; DR。

您好, 所以我认为这是一个非常基本的事情,但它让我头疼:(

我正在编写带有一些图像的JavaFX应用程序。我使用gradle来构建项目。 我的结构是这样的:

src
- main
-- java
--- view
---- sections
----- WelcomeSection.java
-- resources
--- logo.png

- tests
--java
--resources

在遵循gradle包结构之后,在WelcomeSection.java中,我有以下代码片段(请记住它是一个JavaFX应用程序):

Image logo = new Image(getClass().getResourceAsStream("../../../resources/logo.png"));
ImageView logoImageView = new ImageView(logo);

...当我使用Eclipse编译和运行时,它可以很好地工作。

当我尝试通过gradle运行build时,它会生成jar文件。运行jar文件后,我得到以下运行时异常:

Exception in Application start method
java.lang.reflect.InvocationTargetException
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplicationWithArgs(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.launchApplication(Unknown Source)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
        at java.lang.reflect.Method.invoke(Unknown Source)
        at sun.launcher.LauncherHelper$FXHelper.main(Unknown Source)
Caused by: java.lang.RuntimeException: Exception in Application start method
        at com.sun.javafx.application.LauncherImpl.launchApplication1(Unknown Source)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication$155(Unknown Source)
        at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.NullPointerException: Input stream must not be null
        at javafx.scene.image.Image.validateInputStream(Unknown Source)
        at javafx.scene.image.Image.<init>(Unknown Source)
        at main.java.view.sections.WelcomeSection.logoLabel(WelcomeSection.java:83)
        at main.java.view.sections.WelcomeSection.setupTopBar(WelcomeSection.java:73)
        at main.java.view.sections.WelcomeSection.setup(WelcomeSection.java:61)
        at main.java.view.sections.WelcomeSection.<init>(WelcomeSection.java:52)
        at main.java.view.MainView.setupWelcomeSection(MainView.java:27)
        at main.java.view.MainView.setup(MainView.java:37)
        at main.java.view.MainView.<init>(MainView.java:23)
        at main.java.Main.start(Main.java:26)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$162(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$175(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$173(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$174(Unknown Source)
        at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(Unknown Source)
        at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
        at com.sun.glass.ui.win.WinApplication.lambda$null$148(Unknown Source)
        ... 1 more

......这显然与图像路径有关。当我删除图像时,使用gradle构建时它可以正常工作。

TL; DR:我如何在Javafx应用程序中正确引用图像,以便它们在Eclipse和中使用gradle构建时更重要?我尝试在src / main / resources / logo.png的Java代码中更改路径,但这仍然不适用于gradle,也不适用于在Eclipse中运行。

2 个答案:

答案 0 :(得分:1)

如果您的文件直接位于src/main/resources下,则表示它位于类路径的根级别,因此它应为getClass().getResourceAsStream("/logo.png"),如下所示:

Image logo = new Image(getClass().getResourceAsStream("/logo.png"));
ImageView logoImageView = new ImageView(logo);

但请注意,根据Image(String url)格式的构造函数url可以尝试从上下文ClassLoader获取图像,因此您应该能够获得相同的结果用:

Image logo = new Image("/logo.png");
ImageView logoImageView = new ImageView(logo);

答案 1 :(得分:0)

您不应该在代码中引用资源文件夹! src/main/javasrc/main/resources等不属于java项目层次结构,但它们是gradle / maven体系结构的一部分,与内部处理代码和资源的方式有关。

这是构建项目层次结构的一种方法:只需在src/main/java/<path>/WelcomeSection.java中创建与src/main/resources/<path>/相同的层次结构,然后将logo.png放在那里。您可以认为gradle将合并这两个,并且您将在同一目录中拥有WelcomeSection.javalogo.png

 src
- main
-- java
--- view
---- sections
----- WelcomeSection.java
-- resources
---view
----sections
-----logo.png

并在java代码中:

Image logo = new Image(getClass().getResourceAsStream("logo.png"));