-1

Here is the code that I am using:

/**
 * This method loads an image
 * 
 * @param path - path of the image
 * 
 * 
 */
public static BufferedImage loadImage(String path) {
    try {
        return ImageIO.read(ImageLoader.class.getClass().getResourceAsStream(path));
    } catch (IOException e) {
        e.printStackTrace();
        System.exit(1);
    }
    return null;
}

Here is the error:

Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at io.deadspace.graphics.ImageLoader.loadImage(ImageLoader.java:19)
at io.deadspace.graphics.asset.assets.EntityAssets.initEntityAssets(EntityAssets.java:15)
at io.deadspace.graphics.asset.Assets.init(Assets.java:37)
at io.deadspace.Game.init(Game.java:73)
at io.deadspace.Game.run(Game.java:127)
at java.lang.Thread.run(Unknown Source)

I recently re-did the way I loaded images into my game, it worked fine before, but now, it doesn't work anymore. I've tried both getResource and getResourceAsStream.

Here are some examples of how I am loading the images:

public void initEntityAssets() {
    sheet = new SpriteSheet(ImageLoader.loadImage("res/textures/sheet.png"));

    wood = sheet.crop(width, height, width, height);
    tree = sheet.crop(0, 0, width, height * 2);
    rock = sheet.crop(0, height * 2, width, height);
    rockDropItem = sheet.crop(1, height * 2, width, height);

}

public void initHotbarAssets() {
    sheet = new SpriteSheet(ImageLoader.loadImage("res/textures/sheet.png"));

    hotbar = sheet.crop(0, height * 4, width, height);
    hotbar_selected = sheet.crop(0, height * 5, width, height);
}
Thomas Fritsch
  • 9,639
  • 33
  • 37
  • 49

2 Answers2

0

your path should be relative to the class path root, so it should be

sheet = new SpriteSheet(ImageLoader.loadImage("/res/textures/sheet.png"));
Ayo K
  • 1,719
  • 2
  • 22
  • 34
0

If res is inside the java folder of the project structure you must use:

 ImageLoader.class.getClassLoader().getResourceAsStream(path)

The reason is described here:

Difference between getClass().getClassLoader().getResource() and getClass.getResource()?

Another solution instead of using getClassLoader() is adding a slash in the path so it begin searching in the root path.

/res/something

instead of res/something

Marcos Vasconcelos
  • 18,136
  • 30
  • 106
  • 167