Question about ImageIO (first time using it)

So with ImageIcon, you set up the file location like this - “/textures/Player.png”

However, I can’t figure out how to do this with ImageIO. Something I am missing maybe?

Here’s the code that won’t work (this file is inside of the “res” folder, which is already inside my filesystem. The above ImageIcon example works, and that system is set up like this: res >> textures >> file)

File spriteSheetLocation = new File("/spritesheet.png");
BufferedImage bigImg;
	
	public SpriteSheet(Main main)
	{
		try {
			bigImg = ImageIO.read(spriteSheetLocation);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

And a stack trace

javax.imageio.IIOException: Can’t read input file!
at javax.imageio.ImageIO.read(Unknown Source)
at com.natekramber.main.SpriteSheet.(SpriteSheet.java:19)
at com.natekramber.main.Main.(Main.java:41)
at com.natekramber.main.Main.main(Main.java:169)

I know ra4king promotes ImageIO like crazy, maybe he can help :slight_smile:

Thanks a bunch,

-Nathan

That error occurs because it can’t find the file.

Never use File when loading resources, use ImageIO.read(.class.getClassLoader().getResourceAsStream(“res/spritesheet.png”));

Ah ok.

Now I have this -

try {
			bigImg = ImageIO.read(SpriteSheet.class.getClassLoader().getResourceAsStream("/spritesheet.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

but that gives this -

Exception in thread “main” java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at com.natekramber.main.SpriteSheet.(SpriteSheet.java:18)
at com.natekramber.main.Main.(Main.java:41)
at com.natekramber.main.Main.main(Main.java:169)

The interwebz failed to give a quick answer, and I figure you will answer quicker anyways. :smiley:

Thanks,

-Nathan

p.s. It happened whether or not I put /res at the front of it.

input == null also means it can’t find the file. Didn’t you say it’s in “res”?

Edited my previous post, but it was too late :stuck_out_tongue:

I tried it with /res at the front first, and it didn’t work as well. I’ve never had to put that in front before, so I thought maybe it would work without.

Here’s a screenshot that shows my filesystem, as well as the whole class, and the error.

-Nathan

With getClassLoader(), you don’t need the leading forward slash since it automatically uses a path relative to the root of the project.

I actually just discovered this a couple days ago when MatthiasM pointed out that Class.getResource(AsStream) automatically remove the leading forward slash or (if it is absent) pre-pend the Class’s path and then delegate that String to the ClassLoader’s getResource(AsStream). :slight_smile:

Updated with

bigImg = ImageIO.read(SpriteSheet.class.getClassLoader().getResourceAsStream(“res/spritesheet.png”));

Still broken with “input == null!” error.

O’:

-Nathan

OH! You’ve added the separate “res” folder in the build path. This means all you need is “spritesheet.png” without any slashes ;D

Thanks :smiley: