JavaFX 图像空指针异常

标签 java image javafx javafx-2 javafx-8

我试图声明一个 Image 对象,但每次尝试使用它时都会遇到 NullPointerException。这是我的构造函数:

final Image systemSecureImage = new Image((getClass().getResource("images/notifications/systemSecure.png")).toString());

当我尝试在方法中使用它时,它会抛出错误。例如...

sample.sampleMethod(image);

知道我做错了什么吗?我还尝试了其他两种方法:

Image systemSecureImage = new Image("images/notifications/systemSecure.png");
Image systemSecureImage = new Image("file://images/notifications/systemSecure.png");

最佳答案

Image 的构造函数需要资源路径的外部形式。

final Image systemSecureImage = new Image((getClass().getResource("images/notifications/systemSecure.png")).toExternalForm());

关于JavaFX 图像空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22899956/

相关文章:

java - Netbeans - 应用程序重新启动

image - dockerfile 中的 AS 基础和 AS 构建有什么区别?

ios - 不同目标的 Xcode 启动图像

Javafx 音乐播放器不工作

css - 如何动态更改 UI 中的字体大小以在 JavaFX 中始终保持相同的宽度?

java - 云端点不尊重 Jackson 注释

java - 是否可以仅从 wsdl 文件创建 Web 服务客户端?

html - img HTML 标签不支持 HTTP Refresh header (但在浏览器中加载图像 URL)

使用 Maven 的 JavaFX 11 抛出异常 : "WindowsNativeRunloopThread"

java - xuggler 输入输出流处理程序 SIGSEGV