2

I'm making a simple game in Java, and trying to load a sprite from the jar file. I'm using the following code to load a sprite:

spriteURL = getClass().getResource("/res/sprites/sprite_fr1.png");

And if I export a jar file and unzip it, the following folders exist:

/res/sprites/sprite_fr1.png

However, when I try and load the image, I catch a NullPointerException, even though, as far as I can tell, the path exists?

What am I doing wrong here?

EDIT: Screenshot below.

Folder Structure

All sprites are in the folder 'sprites' as PNG files. I'm calling the method from jeu, not jeu/canvases.

Regards, Ben.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
BnMcG
  • 668
  • 9
  • 24
  • Make sure of the case of the packages/filename is correct i.e capital letters where necessary. Windows is not case sensitive however java is. – David Kroukamp Jan 27 '13 at 17:44
  • No capitals in any file names - do I need to include the package name in the getResource() method? Even if the res/ folder is in the top directory of the jar? – BnMcG Jan 27 '13 at 17:48
  • Try to: `spriteURL = this.getClass().getClassLoader().getResource(PATH);` – Maroun Jan 27 '13 at 17:48
  • @BenMagee If *res* is within another package yes. – David Kroukamp Jan 27 '13 at 17:49
  • No such luck with `spriteURL = this.getClass().getClassLoader().getResource("/res/sprites/sprite_fr1.png");`, Maroun. Thanks for the suggestion though. – BnMcG Jan 27 '13 at 17:49
  • @DavidKroukamp - res isn't in any package. Does this affect anything? – BnMcG Jan 27 '13 at 17:49
  • @BenMagee No not as far as I know. Please take a screenshot of your package structure and post it so we can see how your jar file is structured – David Kroukamp Jan 27 '13 at 17:51
  • @BenMagee I saw this as an answer for similar question: `Toolkit.getDefaultToolkit().getImage(getClass().getResource(PATH));` – Maroun Jan 27 '13 at 17:52
  • @MarounMaroun - Your suggestion still results in a null pointer exception. – BnMcG Jan 27 '13 at 17:56
  • @BenMagee If you're sure that classloader uses this specific jar, then I'm clueless. (It might be that your classloader using files from somewhere else and not this jar) – Maroun Jan 27 '13 at 17:58
  • @DavidKroukamp Screenshot posted. – BnMcG Jan 27 '13 at 17:58
  • 1
    Fixed by looking at Deniz's suggestion of build path. sprites/ was excluded from the build path, and even though the png's were still appearing in the jar java didn't seem to be able to find them. Fixed now. Thanks for everybody's help! – BnMcG Jan 27 '13 at 18:05
  • @BenMagee : Please have a look at this [answer](http://stackoverflow.com/a/9866659/1057230). – nIcE cOw Jan 28 '13 at 01:49

1 Answers1

2

Check classpath of your project in your IDE. For example in eclipse there is configuration for each folder (Properties->JAva Build Path->Source Tab). It may be set to exclude png files.

Deniz
  • 1,575
  • 1
  • 16
  • 27
  • Hi Deniz, I can see the png's in the jar file though, under the directory specified. It also doesn't work if I run debug from eclipse. – BnMcG Jan 27 '13 at 17:44