[英]IllegalArgumentException : image must be non-null
I'm working on a simple "Space Invaders" game using JavaFx, and while my game works fine without any exceptions or errors when I run the project inside.netbeans, it keeps giving me this error when I run the.jar file through CMD and I can't seem to find a solution.我正在使用 JavaFx 开发一个简单的“太空入侵者”游戏,虽然我的游戏在运行 inside.netbeans 项目时没有任何异常或错误,但当我通过 CMD 运行 .jar 文件时,它一直给我这个错误而且我似乎找不到解决方案。
//this is an example of how i define my images/imagepattern //这是我如何定义图像/图像模式的示例
Image il=new Image("file:ship.png");
setFill(new ImagePattern(il));
// this is the error // 这是错误
Exception in thread "JavaFX Application Thread" java.lang.IllegalArgumentException: Image must be non-null
at com.sun.prism.paint.ImagePattern.<init>(ImagePattern.java:44)
at com.sun.javafx.tk.quantum.QuantumToolkit.createImagePatternPaint(QuantumToolkit.java:905)
at com.sun.javafx.tk.Toolkit.getPaint(Toolkit.java:657)
at javafx.scene.paint.ImagePattern.acc_getPlatformPaint(ImagePattern.java:291)
at javafx.scene.paint.Paint$1.getPlatformPaint(Paint.java:51)
at javafx.scene.shape.Shape.updatePGShape(Shape.java:916)
at javafx.scene.shape.Shape.impl_updatePeer(Shape.java:965)
at javafx.scene.shape.Rectangle.impl_updatePeer(Rectangle.java:541)
at javafx.scene.Node.impl_syncPeer(Node.java:503)
at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2304)
at javafx.scene.Scene$ScenePulseListener.syncAll(Scene.java:2313)
at javafx.scene.Scene$ScenePulseListener.synchronizeSceneNodes(Scene.java:2280)
at javafx.scene.Scene$ScenePulseListener.pulse(Scene.java:2419)
at com.sun.javafx.tk.Toolkit.lambda$runPulse$29(Toolkit.java:398)
at java.security.AccessController.doPrivileged(Native Method)
at com.sun.javafx.tk.Toolkit.runPulse(Toolkit.java:397)
at com.sun.javafx.tk.Toolkit.firePulse(Toolkit.java:424)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:518)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulse(QuantumToolkit.java:498)
at com.sun.javafx.tk.quantum.QuantumToolkit.pulseFromQueue(QuantumToolkit.java:491)
at com.sun.javafx.tk.quantum.QuantumToolkit.lambda$runToolkit$408(QuantumToolkit.java:319)
at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
at com.sun.glass.ui.win.WinApplication.lambda$null$152(WinApplication.java:177)
at java.lang.Thread.run(Unknown Source)
EDIT: after some debugging i have found that the.jar looks for the images in C:\Users\Admin instead of within the project itself, how can i change that?编辑:经过一些调试后,我发现 .jar 在 C:\Users\Admin 中查找图像,而不是在项目本身中,我该如何更改它?
I believe that the image should be in the same directory as your java project or declare a package of icons inside your project.我相信图像应该与您的 java 项目位于同一目录中,或者在您的项目中声明一个图标包。 eg:
/icons/monimage.png
in the source directory otherwise your access path will tighten例如:源目录中的
/icons/monimage.png
否则您的访问路径会收紧
Image il=new Image("/icon/monimage.png");
setFill(new ImagePattern(il));
public static void SetImage(ImageView imgV, String patch) throws FileNotFoundException {
File file = new File(patch);
Image image = new Image(file.toURI().toString(), imgV.getFitHeight(), imgV.getFitWidth(), true, true);
imgV.setImage(image);
imgV.setPreserveRatio(true);
}
You can use this function to display an image directly in an ImagaView您可以使用此 function 直接在 ImagaView 中显示图像
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.