我如何修复IllegalArgumentException?

时间:2014-12-11 01:31:11

标签: java applet japplet

我正在编写一个游戏并向我发出此错误。

我该如何解决?

出现 IllegalArgumentException 的代码位于 SpriteScreen.java


SpriteScreen.java

package game.gfx;

import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;

public class SpriteSheet {

    public String path;
    public int width, height;

    public int[] pixels;

    public SpriteSheet(String path) {
        BufferedImage image = null;

        try {
            image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path)); //THIS IS LINE #18 (WHERE THE ERROR IS OCCURING)!
        } catch (IOException e) {
            e.printStackTrace();
        }

        if (image == null) {
            return;
        }

        this.path = path;
        this.width = image.getWidth();
        this.height = image.getHeight();

        pixels = image.getRGB(0, 0, width, height, null, 0, width);

        for (int i = 0; i < pixels.length; i++) {
            pixels[i] = (pixels[i] * 0xff) / 64;
        }

        for (int i = 0; i < 8; i++) {
            System.out.println(pixels[i]);
        }
    }
}

错误

Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at game.gfx.SpriteSheet.<init>(SpriteSheet.java:18)
at game.Game.<init>(Game.java:24)
at game.Game.main(Game.java:130)

2 个答案:

答案 0 :(得分:0)

代码似乎没问题。我认为问题在于道路。您确定在输入中放置了正确的路径吗?尝试使用&#34; /&#34;开始路径。例如&#34; /resources/sprite_sheet.PNG"

答案 1 :(得分:-2)

试着希望它能解决你的问题:

   if (image.equal("")) {
     return;
  }