如何使用javafx设置图像

时间:2015-09-28 14:45:39

标签: java javafx

使用此代码设置图像时出错。 我还把我的图像放在src文件夹中。我尝试了另一种方式但没有任何方法可以帮助

Error:


    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$152(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$50/14845382.run(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 Clock.start(Clock.java:26)
        at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(Unknown Source)
        at com.sun.javafx.application.LauncherImpl$$Lambda$53/2934105.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$45/18503843.run(Unknown Source)
        at com.sun.javafx.application.PlatformImpl.lambda$null$170(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$48/19884472.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(Unknown Source)
        at com.sun.javafx.application.PlatformImpl$$Lambda$47/2180324.run(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$145(Unknown Source)
        at com.sun.glass.ui.win.WinApplication$$Lambda$36/3326003.run(Unknown Source)
        ... 1 more
    Exception running application Clock
下面的

错误每次运行代码。

Home  |  *Work*  |  Contact  |  . . .

帮我解决这个问题。 谢谢

2 个答案:

答案 0 :(得分:1)

Caused by: java.lang.NullPointerException: Input stream must not be null
...
at Clock.start(Clock.java:26)

您在第26行获得了NPE,这是以下代码行:

ImageView image = new ImageView(new Image(getClass().getResourceAsStream("analog.png")));

罪魁祸首可能是参考 analog.png。系统无法从您提供的位置读取文件,getResourceAsStream()正在返回null值。

您可以通过将这一行排成一行来进一步确定:

InputStream inStream = getClass().getResourceAsStream("analog.png");
Image imageObject = new Image(inStream);
ImageView image = new ImageView(imageObject);  

并确认运行Clock时新堆栈跟踪引用了哪个行号。

确认您的文件analog.png可访问或更正图像文件沿路径的路径/位置。 getResourceAsStream()中汇总了nullresult = copyInfo(argv[1], argv[2]); 返回时正确使用的规则。

答案 1 :(得分:1)

您的InputStreamnull。你应该知道,如果你的Image文件在你的源文件夹中,那么你应该将它指向它。右键单击您的图像并选择copy qualified name并将其用作示例

我的src文件夹中有一个文件夹 - (包)名为files,我的图像文件是default_profile_icon.png,然后我将使用此代码

new Image("/files/default_profile_icon.png",160,160,false,true)

第一个参数就是你所看到的,希望你现在能够得到它。

我不知道您的图片位于何处,因此这个答案