views:

36

answers:

1

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)
+1  A: 

Apparently you don't have a w0.gif file. Either create one or let array index start at 1.

BalusC
Oh, good catch! Thanks!
Dan