Getting a IIOException

javax.imageio.IIOException: Can't read input file!
	at javax.imageio.ImageIO.read(ImageIO.java:1275)
	at game.Game.initEntities(Game.java:77)
	at game.Game.<init>(Game.java:65)
	at game.Game.main(Game.java:136)
Exception in thread "main" java.lang.NullPointerException
	at game.Game.gameLoop(Game.java:87)
	at game.Game.main(Game.java:138)

I keep receiving this error, line 77 of Game.java is -


player = new Player(1, 8, ImageIO.read(new File("img/player.png")));

The Player class is in the game package and the image is in the img package (two separate directories).

What does new File(“img/player.png”).exists() return?

Kev

Try adding a “/” before the “img”. This is because you want to start at the root directory, which is the “bin” of Eclipse.

This is incorrect. It will point to the root of your filesystem.

Figured it out, needed to use “src/img/player.png” because my classpath isn’t in the same folder.

Watch over your package too. Src/res folder should be in same place with root folder of package.

Oh right! Using File will cause “/” to point to the root of the file system. Using “/” in System.getResource points to the CWD

Are you thinking of getSystemResoucre() or getResource()? I’m not sure it works like that. I think (I don’t have my computer right now so I can’t check) I use getClass().getClassLoader().getResource(“images/logo.png”); and my file structure in Eclipse is “ProjectFolder -> src -> images -> logo.png” and my source is at “ProjectFolder -> src -> com -> Main.java”. That way when I create a .jar it’s “MyProject.jar -> images -> logo.png”