JavaFX图像nullpointerexception
我试图声明一个图像对象,但每次尝试使用它时都会得到一个NullPointerException。这是我的构造器:JavaFX图像nullpointerexception,java,image,javafx,javafx-2,javafx-8,Java,Image,Javafx,Javafx 2,Javafx 8,我试图声明一个图像对象,但每次尝试使用它时都会得到一个NullPointerException。这是我的构造器: final Image systemSecureImage = new Image((getClass().getResource("images/notifications/systemSecure.png")).toString()); 当我尝试在方法中使用它时,它会抛出错误。例如 sample.sampleMethod(image); 知道我做错了什么吗?我还尝试了其他两种
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");
映像的构造函数需要资源路径的外部形式
final Image systemSecureImage = new Image((getClass().getResource("images/notifications/systemSecure.png")).toExternalForm());
是否确实由于映像而引发NullPointerException?
如果这条线
sample.sampleMethod(image);
抛出该NPE,这是因为sample
为空,即未启动。仔细观察异常堆栈跟踪,并准确确定异常发生在哪一行。您必须创建一个新的源文件夹,以便javafx找到您的图像;这在JavaFX2和JavaFX8中都适用
假设你有这个项目:
project/src
project/lib
project/img
project/img/image.png
只需右键单击eclipse->Build Path->Use as source文件夹中的img
文件夹
您的图像将是这样的:新图像(“image.png”)
希望有帮助,
Laurenz看一下的:-)这个问题在JDK1.8中得到了解决…试试最新的jdkI,我仍然会遇到同样的错误,原因是:java.lang.NullPointerException我实际上在使用NetBeans。我没有使用Netbeans,但如果我没有弄错的话,这对你来说应该是可行的:右键单击你的项目->单击“属性”->,然后你应该看到“源”高亮显示->单击“添加文件夹…”,其中显示“源程序包文件夹”