0

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
  • Print out the String.format("weapon/w%d.gif", xi)) , ther's probably an image file missing. Commented Jul 14, 2010 at 22:39

1 Answer 1

1

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

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.