Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/387.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
用于数组的java_Java_Image_Arrays_Applet - Fatal编程技术网

用于数组的java

用于数组的java,java,image,arrays,applet,Java,Image,Arrays,Applet,我正在尝试修复一个数组问题,它将图像存储到数组中,因此我不必单独执行 这是我的代码: 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 <

我正在尝试修复一个数组问题,它将图像存储到数组中,因此我不必单独执行

这是我的代码:

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)));
}

显然您没有
w0.gif
文件。创建一个或让数组索引从
1

开始打印字符串.format(“武器/w%d.gif”,xi)),可能缺少一个图像文件。
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)