TheGamerPlayz TheGamerPlayz - 2 months ago 15
Java Question

BufferedImage not loading from png

I am trying to load an image from a spritesheet and its been a while since I've made a 2d game, so I think I probably forgot something important. I have

res
added to the class folder and have been trying for quite a while to load it with no success. I may have loaded it into the incorrectly buildpath, but I don't think so.
Here is the error:

Exception in thread "Thread-2" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at me.xthegamerplayz.Dodger.graphics.ImageLoader.loadImage(ImageLoader.java:12)
at me.xthegamerplayz.Dodger.graphics.Assets.init(Assets.java:15)
at me.xthegamerplayz.Dodger.Dodger.init(Dodger.java:30)
at me.xthegamerplayz.Dodger.Dodger.run(Dodger.java:88)
at java.lang.Thread.run(Unknown Source)


And here is where I am loading the image

public static BufferedImage loadImage(String path){
try {
return ImageIO.read(ImageLoader.class.getResource(path));
}
catch (IOException e) {
e.printStackTrace();
System.exit(1);
}
return null;
}


And finally this is where I am loading the images:

public static void init() {
sheet = new SpriteSheet(ImageLoader.loadImage("/res/entities/entitySprites.png"));

player = sheet.crop(0, 0, width, height);
enemy = sheet.crop(1, 0, width, height);
}


Does anyone see a reason why it is not loading?

Answer

It cannot find the resource. You can inspect the jar as it is in zip format. Most likely the path starts wrong. It also is case-sensitive.

sheet = new SpriteSheet(ImageLoader.loadImage("/entities/entitySprites.png"));
Comments