N1els5
Senior Newbie 
|
 |
«
Posted
2012-08-26 15:23:36 » |
|
Hey, Im making a game with slick and lwjgl only I've got this error: 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) From this piece of code: This is how I load the textures: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| 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! 
|
|
|
|
jonjava
|
 |
«
Reply #1 - Posted
2012-08-26 15:42:47 » |
|
Is the blocks folder in your build path?
Project -> Properties -> Java Build Path -> [left most tab] -> add folder -> blocks
|
|
|
|
ra4king
|
 |
«
Reply #2 - Posted
2012-08-26 18:41:43 » |
|
When you add the blocks folder to the build path, you have to remove "blocks/" from all the specified paths.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
jonjava
|
 |
«
Reply #3 - Posted
2012-08-26 19:33:50 » |
|
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: 1 2 3 4 5 6 7
| src/ bla bla res/ 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. 1 2 3 4 5 6 7
| res/ gfx/ tileset.png snd/ jumpSound.wav music/ still_alive.mp3 |
|
|
|
|
N1els5
Senior Newbie 
|
 |
«
Reply #4 - Posted
2012-08-27 08:47:06 » |
|
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. 
|
|
|
|
gimbal
|
 |
«
Reply #5 - Posted
2012-08-27 09:08:49 » |
|
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...
|
|
|
|
N1els5
Senior Newbie 
|
 |
«
Reply #6 - Posted
2012-08-27 09:15:51 » |
|
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?
|
|
|
|
jonjava
|
 |
«
Reply #7 - Posted
2012-08-27 09:33:18 » |
|
Hmm try refreshing your project? right click -> refresh.
Or try what ra4king said and remove the 'blocks/' part and see if that works.
|
|
|
|
N1els5
Senior Newbie 
|
 |
«
Reply #8 - Posted
2012-08-27 10:02:12 » |
|
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
|
|
|
|
PeterNicholson
|
 |
«
Reply #9 - Posted
2012-08-27 10:05:09 » |
|
Hi! Try this: 1 2 3 4 5 6 7 8 9 10 11 12
| 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!
|
|
|
|
Games published by our own members! Check 'em out!
|
|
PeterNicholson
|
 |
«
Reply #10 - Posted
2012-08-27 10:08:39 » |
|
Replace 1
| leaves = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/leaves.png")); |
with 1
| leaves = TextureLoader.getTexture("PNG", new FileInputStream(new File("blocks/leaves.png"))); |
. Do the same to every texture!
|
|
|
|
N1els5
Senior Newbie 
|
 |
«
Reply #11 - Posted
2012-08-27 10:12:56 » |
|
Hi! Try this: 1 2 3 4 5 6 7 8 9 10 11 12
| 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 1
| leaves = TextureLoader.getTexture("PNG", ResourceLoader.getResourceAsStream("blocks/leaves.png")); |
with 1
| leaves = TextureLoader.getTexture("PNG", new FileInputStream(new File("blocks/leaves.png"))); |
. Do the same to every texture! No, I'll get the same error: 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)
|
|
|
|
N1els5
Senior Newbie 
|
 |
«
Reply #12 - Posted
2012-08-27 10:21:54 » |
|
It, works! Thank you! 
|
|
|
|
PeterNicholson
|
 |
«
Reply #13 - Posted
2012-08-27 10:22:29 » |
|
Your`e Welcome 
|
|
|
|
ra4king
|
 |
«
Reply #14 - Posted
2012-08-27 21:02:18 » |
|
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 <MyClass>.class.getResourceAsStream("/blocks/leaves.png");
EDIT: right, forgot the leading slash.
|
|
|
|
sproingie
|
 |
«
Reply #15 - Posted
2012-08-27 23:02:33 » |
|
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.
|
|
|
|
gimbal
|
 |
«
Reply #16 - Posted
2012-08-29 08:27:59 » |
|
DOH, I didn't even see that. getClass().getClassLoader().getResourceAsStream() should also work.
|
|
|
|
|