Nickels
Nickels

Reputation: 15

Java: Loading BufferedImage from res Folder

Im trying to load an image from my res folder which is already part of the Java BuildPath. Sadly it seems I am no able to find the image neither with relative nor with an absolute path to the file. Im always getting this error message:

Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
 at javax.imageio.ImageIO.read(Unknown Source)
 at schneider.twodgame.BufferedImageLoader.loadImage(BufferedImageLoader.java:14)
 at schneider.twodgame.Game.init(Game.java:64)
 at schneider.twodgame.Game.run(Game.java:99)
 at java.lang.Thread.run(Unknown Source)

And here is a part of the code:

public class BufferedImageLoader {

private BufferedImage image;

public BufferedImage loadImage(String path) throws IOException {
    System.out.println(getClass());
    image = ImageIO.read(getClass().getResource(path));
    return image;
    }
}

This is the method I am trying to load the image with. The method is part of my Main Class:

public void init() {
    BufferedImageLoader loader = new BufferedImageLoader();

    try {
        spriteSheet = loader.loadImage("/res/sprite_sheet.png");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

Upvotes: 1

Views: 965

Answers (2)

Maarten Bodewes
Maarten Bodewes

Reputation: 93988

It is currently looking in your .class file folder or .jar file at the location of your class files in:

[root folder of class files]/res/sprite_sheet.png

Maybe it should be looking in:

[root folder of class files]/schneider/twodgame/res/sprite_sheet.png

In that case you should remove the leading slash (/).

Upvotes: 0

Doon
Doon

Reputation: 3749

Have a look here.

spriteSheet = loader.loadImage("/sprite_sheet.png");

Should work.

Upvotes: 1

Related Questions