为什么getClass()。getResourceAsStream(file.getAbsolutePath())抛出nullPointerException?

问题描述 投票:-1回答:1

[我已经找到问题的解决方案并阅读了比我的专业还要专业的类似问题,...好吧,尽管看起来很简单,但我还是希望您能注意我的问题!我正在研究一个由FileChooser打开文件的项目,然后尝试在窗格上显示它。问题是getClass()。getResourceAsStream(file.getAbsolutePath())返回null,因此虽然我可以打印路径并看到它是真实的,但不能在创建图像时使用它。我的代码的一部分是:

    FileChooser fileChooser = new FileChooser();
    File file = fileChooser.showOpenDialog(stage);

...

     Image img = new Image(getClass().getResourceAsStream(file.getAbsolutePath());

例外是:

    Exception in thread "JavaFX Application Thread" java.lang.NullPointerException: Input stream must not be null ...

我通过NetBeans在Ubuntu上工作。我真的很乐于助人。谢谢。

java image nullpointerexception inputstream filechooser
1个回答
0
投票

使用ImageIO:

© www.soinside.com 2019 - 2024. All rights reserved.