Java缓冲图像未加载
我想要加载这个图像,但它总是给我一个 input= null 异常。 这是代码的第一部分:
Entity e = new Entity("images/meganium.png");
这是加载图像的部分:
image = null;
try{
path = this.getClass().getResource(fileName);
System.out.println(path);
image = ImageIO.read(path);
}catch(IOException e){
System.out.println("Dun goofed in " + "SPrites");
}
结构如下:
com/blah/bleh/Main
com/blah/bleh/images
com/blah/bleh/foo/bar/Loader Class
堆栈跟踪:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1362)
at net.ofn.nyc.javagentleman.src.graphic.Sprite.<init>(Sprite.java:31)
at net.ofn.nyc.javagentleman.src.ent.Entity.<init>(Entity.java:21)
at net.ofn.nyc.javagentleman.JavaGentleman.<init>(JavaGentleman.java:27null)
at net.ofn.nyc.javagentleman.JavaGentleman.main(JavaGentleman.java:23)
I have this image that I want to load but it always gives me an input= null exception.
This is the first bit of code:
Entity e = new Entity("images/meganium.png");
Here is the part that loads the image:
image = null;
try{
path = this.getClass().getResource(fileName);
System.out.println(path);
image = ImageIO.read(path);
}catch(IOException e){
System.out.println("Dun goofed in " + "SPrites");
}
The structure is like this:
com/blah/bleh/Main
com/blah/bleh/images
com/blah/bleh/foo/bar/Loader Class
Stack trace:
java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(ImageIO.java:1362)
at net.ofn.nyc.javagentleman.src.graphic.Sprite.<init>(Sprite.java:31)
at net.ofn.nyc.javagentleman.src.ent.Entity.<init>(Entity.java:21)
at net.ofn.nyc.javagentleman.JavaGentleman.<init>(JavaGentleman.java:27null)
at net.ofn.nyc.javagentleman.JavaGentleman.main(JavaGentleman.java:23)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您是否有 2 个独立的目录,一个名为
images
,另一个名为Images
(大写“I”)?保持一致。您的文件系统可能无法区分这两者,但 Java 可以。Do you have 2 separate directories, one named
images
the other namedImages
(capital 'I')? Keep it consistent. Your file system may not distinguish between the two, but Java does.您正在使用图像资源的相对路径。与 类。 getResource(),相对路径是针对包含该类的包进行解析的,因此如果加载图像的类位于包 com.blah.bleh.foo.bar 中,那么它将在以下位置查找图像/com/blah/bleh/foo/bar/images/meganium.png。如果 getResource() 找不到给定的资源,则返回 null,从而导致 IllegalArgumentException。
You're using a relative path for the image resource. With Class.getResource(), relative paths are resolved against the package containing the class, so if the class loading the images is in package com.blah.bleh.foo.bar, then it will be looking for the image at /com/blah/bleh/foo/bar/images/meganium.png. getResource() returns null if it can't find the given resource and hence your IllegalArgumentException.