我试图声明一个 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/