java 用于数组
我正在尝试解决将图像存储到数组中的数组问题,这样我就不必单独执行此操作。
这是我的代码:
tiles = new Image[NUM_TILES];
for (int i = 0; i < NUM_TILES; i++) {
tiles[i] = getImage(getClass().getResource(String.format("tiles/t%d.png", i)));
}
weapon = new Image[2];
for (int xi = 0; xi < 2; xi++) {
weapon[xi] = getImage(getClass().getResource(String.format("weapon/w%d.gif", xi)));
}
您看到底部的 weapon/w%d.gif
了吗?这就是问题所在。当我将 %d
替换为 w1.gif
等文件夹中的武器文件而不是 w%d.gif
时,它就可以工作了。但我希望它加载我所有的武器文件图像。它编译得很好,但是当我启动它时,我收到此错误
java.lang.NullPointerException
at sun.awt.image.URLImageSource.<init>(URLImageSource.java:29)
at sun.applet.AppletImageRef.reconstitute(AppletImageRef.java:33)
at sun.misc.Ref.get(Ref.java:47)
at sun.applet.AppletViewer.getCachedImage(AppletViewer.java:377)
at sun.applet.AppletViewer.getImage(AppletViewer.java:372)
at java.applet.Applet.getImage(Applet.java:242)
at tileGen.init(tileGen.java:51)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:619)
I am trying to fix a array problem where it stores images into arrays so I don't have to do it individualy.
Here my code:
tiles = new Image[NUM_TILES];
for (int i = 0; i < NUM_TILES; i++) {
tiles[i] = getImage(getClass().getResource(String.format("tiles/t%d.png", i)));
}
weapon = new Image[2];
for (int xi = 0; xi < 2; xi++) {
weapon[xi] = getImage(getClass().getResource(String.format("weapon/w%d.gif", xi)));
}
You see the weapon/w%d.gif
at the bottom? That's the problem. When I replace the %d
with a weapon file from the folder like w1.gif
instead of w%d.gif
it works. But I want it to load all my weapon file images. It compiles fine but when I go to launch it I get this error
java.lang.NullPointerException
at sun.awt.image.URLImageSource.<init>(URLImageSource.java:29)
at sun.applet.AppletImageRef.reconstitute(AppletImageRef.java:33)
at sun.misc.Ref.get(Ref.java:47)
at sun.applet.AppletViewer.getCachedImage(AppletViewer.java:377)
at sun.applet.AppletViewer.getImage(AppletViewer.java:372)
at java.applet.Applet.getImage(Applet.java:242)
at tileGen.init(tileGen.java:51)
at sun.applet.AppletPanel.run(AppletPanel.java:424)
at java.lang.Thread.run(Thread.java:619)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
显然您没有
w0.gif
文件。创建一个数组或让数组索引从1
开始。Apparently you don't have a
w0.gif
file. Either create one or let array index start at1
.