Problem with loading resources

Hey,

Im making a game with slick and lwjgl only I’ve got this error:

[quote]Exception in thread “main” java.lang.NullPointerException
at Lost.Adventure.WorldGenBasic.build(WorldGenBasic.java:198)
at Lost.Adventure.Mainclass.start(Mainclass.java:45)
at Lost.Adventure.Mainclass.main(Mainclass.java:78)
[/quote]
From this piece of code:

stone.bind();

This is how I load the textures:

static Texture stone;
	static Texture dirt;
	static Texture grass;
	static Texture log;
	static Texture leaves;
	public void init() throws IOException 
	{
		try {
		stone = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/stone.png"));
		dirt = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/dirt.png"));
		grass = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/grass.png"));
		log = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/log.png"));
		leaves = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/leaves.png"));
		} catch (IOException e) {
			e.printStackTrace();
		}
	}

And this is my workspace:

Project -> scr -> Lost.adventure -> WorldGenBasic.java (java files)
Project -> blocks -> stone.png (resources)

How does I fix this error?

Thank you in advance! :slight_smile:

Is the blocks folder in your build path?

Project -> Properties -> Java Build Path -> [left most tab] -> add folder -> blocks

When you add the blocks folder to the build path, you have to remove “blocks/” from all the specified paths.

If you have another top folder called, for example, “res” - you can insert your blocks folder inside that top res folder and add the res folder to your build path, that way, iirc, you can leave your ‘blocks/’ in the path name to specify their exact locations.

So it’d look like this in eclipse:

src/
   bla bla
res/ // add this to build path
    blocks/
            image.png
            anotherimage.png
            sprite.png

This way you don’t have to add every new folder to the build path but simply add everything inside your resource ( res ) folder like sound, graphics, music etc.

res/
    gfx/
        tileset.png
    snd/
        jumpSound.wav
    music/
        still_alive.mp3

Hi,

Thanks for your reaction. Now I’ve added the blocks folder into the resources folder and add the resources folder to the buildpath. But he doesn’t work, I’ll get the same error.

Check the Eclipse project build path - make sure that the resources are not excluded by default, or the other way around that there is no inclusion of '.java on it. Yes - that happens in some Eclipse project setups…

Thank you for your response, but I don´t understand it (Im a noob in resources I’ve never worked with it before ::))

This is a picture of my buildpath:

I think the resource folder isn’t excluded by default?

Hmm try refreshing your project? right click -> refresh.

Or try what ra4king said and remove the ‘blocks/’ part and see if that works.

Nope, both don’t work

Hi!
Try this:


try {
            logo = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/logo.png")));
            menu_start = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/menu_start.png")));
            menu_exit = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/menu_exit.png")));
            stone = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/stone.png")));
            background = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/background.png")));
            grass = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/grass.png")));
            carpet = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/carpet.png")));
            player = TextureLoader.getTexture("PNG", new FileInputStream(new File("res/player.png")));
        } catch (IOException ex) {
            Logger.getLogger(textureLoader.class.getName()).log(Level.SEVERE, null, ex);
        }

Put that in you`re texture loader! Hope it helps!

Replace

leaves = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/leaves.png"));

with

leaves = TextureLoader.getTexture("PNG", new FileInputStream(new File("blocks/leaves.png")));

.
Do the same to every texture!

No, I’ll get the same error:

[quote]Exception in thread “main” java.lang.NullPointerException
at Lost.Adventure.WorldGenBasic.build(WorldGenBasic.java:198)
at Lost.Adventure.Mainclass.start(Mainclass.java:50)
at Lost.Adventure.Mainclass.ask(Mainclass.java:93)
at Lost.Adventure.Mainclass.main(Mainclass.java:68)
[/quote]

It, works! Thank you! ;D

Your`e Welcome ;D

Whoa! Don’t use File! You’re most likely going to distribute this application a JAR and File only works on the file system. Use .class.getResourceAsStream("/blocks/leaves.png");

EDIT: right, forgot the leading slash.

Using Class.getResourceAsStream will resolve relative to the class’s location unless you use a leading slash.

If you’re using Slick, you should be using ResourceLoader, which searches the classpath by default. I suspect it’s a build problem. Try doing a clean and full rebuild.

DOH, I didn’t even see that. getClass().getClassLoader().getResourceAsStream() should also work.