Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (546)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Problem with loading resources  (Read 3556 times)
0 Members and 1 Guest are viewing this topic.
Offline 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:
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)

From this piece of code:
1  
stone.bind();


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! Smiley
Offline 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

Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« 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!
Legends of Yore - The Casual Retro Roguelike
Offline 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/ // 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.

1  
2  
3  
4  
5  
6  
7  
res/
    gfx/
        tileset.png
    snd/
        jumpSound.wav
    music/
        still_alive.mp3

Offline 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.
Offline gimbal

JGO Knight


Medals: 25



« 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...
Offline 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 Roll Eyes)

This is a picture of my buildpath:

I think the resource folder isn't excluded by default?
Offline 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.

Offline 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
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« 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!

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« 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!

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline 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:
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)
Offline N1els5

Senior Newbie





« Reply #12 - Posted 2012-08-27 10:21:54 »

It, works! Thank you! Grin
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #13 - Posted 2012-08-27 10:22:29 »

Your`e Welcome  Grin

Subscribe to me on YouTube: http://youtube.com/javaprogrammerguy
Offline ra4king

JGO Kernel


Medals: 345
Projects: 2
Exp: 5 years


I'm the King!


« 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.

Offline sproingie

JGO Kernel


Medals: 202



« 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.
 
Offline gimbal

JGO Knight


Medals: 25



« Reply #16 - Posted 2012-08-29 08:27:59 »

DOH, I didn't even see that. getClass().getClassLoader().getResourceAsStream() should also work.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

atombrot (20 views)
2014-08-19 09:29:53

Tekkerue (21 views)
2014-08-16 06:45:27

Tekkerue (21 views)
2014-08-16 06:22:17

Tekkerue (12 views)
2014-08-16 06:20:21

Tekkerue (19 views)
2014-08-16 06:12:11

Rayexar (55 views)
2014-08-11 02:49:23

BurntPizza (37 views)
2014-08-09 21:09:32

BurntPizza (27 views)
2014-08-08 02:01:56

Norakomi (35 views)
2014-08-06 19:49:38

BurntPizza (64 views)
2014-08-03 02:57:17
List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59:08
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!