Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (406)
games submitted by our members
Games in WIP (293)
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 2223 times)
0 Members and 1 Guest are viewing this topic.
Offline N1els5

Senior Newbie





« Posted 2012-08-26 17: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

JGO Knight


Medals: 32



« Reply #1 - Posted 2012-08-26 17:42:47 »

Is the blocks folder in your build path?

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

Online ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #2 - Posted 2012-08-26 20: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!
Try the Free Demo of Titan Attacks
Offline jonjava

JGO Knight


Medals: 32



« Reply #3 - Posted 2012-08-26 21: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 10: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 Coder


Medals: 15



« Reply #5 - Posted 2012-08-27 11: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 11: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

JGO Knight


Medals: 32



« Reply #7 - Posted 2012-08-27 11: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 12: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 12: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!
Legends of Yore - The Casual Retro Roguelike
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



« Reply #10 - Posted 2012-08-27 12: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!
Offline N1els5

Senior Newbie





« Reply #11 - Posted 2012-08-27 12: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 12:21:54 »

It, works! Thank you! Grin
Offline PeterNicholson

Senior Member


Medals: 3
Projects: 1



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

Your`e Welcome  Grin
Online ra4king

JGO Kernel


Medals: 264
Projects: 2


I'm the King!


« Reply #14 - Posted 2012-08-27 23: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
« Reply #15 - Posted 2012-08-28 01: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 Coder


Medals: 15



« Reply #16 - Posted 2012-08-29 10: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.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

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

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (81 views)
2013-05-17 21:29:12

alaslipknot (91 views)
2013-05-16 21:24:48

gouessej (122 views)
2013-05-16 00:53:38

gouessej (114 views)
2013-05-16 00:17:58

theagentd (126 views)
2013-05-15 15:01:13

theagentd (113 views)
2013-05-15 15:00:54

StreetDoggy (158 views)
2013-05-14 15:56:26

kutucuk (180 views)
2013-05-12 17:10:36

kutucuk (180 views)
2013-05-12 15:36:09

UnluckyDevil (187 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
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!
Page created in 0.1 seconds with 21 queries.