[SOLVED] Newbie Question - Java Null Pointer Exception

Hi guys,

I am learning jME and am facing something really trivial - in the code for "HelloTerrain.java" in complex Terrain, I want to use ImageBasedHeightMap and as soon as I change the image in the very first two lines in complexTerrain - from bubble to image.jpg

"URL grayscale = HelloTerrain.

class.getClassLoader(). getResource("jmetest/data/texture/image.jpg");" - where image.jpg is white maze generated on black canvas - 200 x 200

I get a Null Pointer Exception as below:

"SEVERE: Exception in game loop


        at javax.swing.ImageIcon.<init>(ImageIcon.java:138)

        at ares.HelloTerrain.complexTerrain(HelloTerrain.java:105)

        at ares.HelloTerrain.simpleInitGame(HelloTerrain.java:39)

        at com.jme.app.BaseSimpleGame.initGame(Unknown Source)

        at com.jme.app.BaseGame.start(Unknown Source)

        at ares.HelloTerrain.main(HelloTerrain.java:30)"

Can anyone please tell me why do I get this?

Kind Regards,


Your program can't find the image.jpg file.

Make sure that image.jpg is in the jmetest/data/texture package or workspace folder. If you use Eclipse, you might have to right click on the jmetest.data.textures package in the Package Explorer and do "refresh" before it appears in the package.

Hope that helps!

Hi ninja/john ninjajohn :slight_smile:

I agree there. I understand that the program cant find image.jpg. Let me give more details about image.jpg - image.jpg is a maze generated using Prim's algorithm and saved using BufferedImage and ImageIO. So I run maze.java which generates the maze and saves it as a 200 x 200 jpeg file in the jmetestdatatexture folder(I check that folder and everytime a new maze is generated i.e. the image.jpg is there and is updated everytime maze.java is run. Now I run HelloTerrain.java with the name of the image in the complex terrain changed to:

"URL grayscale = HelloTerrain.

class.getClassLoader(). getResource("jmetest/data/texture/image.jpg");"

But for some reason it does not recognise image.jpg and gives me a null pointer exception as you point out.

So what is going wrong here? If I can use this image then I can generate a good maze environment - which can be enhanced and can be a really good background.



What you should do then is check if the URL is the one that is null… Also, the new ResourceLocator class could help you with loading textures and the like. Take a look at it. http://www.jmonkeyengine.com/jmeforum/index.php?topic=5707.0

If you already have the image in BufferedImage format, why not pass that directly to the TextureManager instead of saving it out and reloading it?

Hi guys - duenez and renanse,

Thanks for your replies.

duenez - I checked for the URL of the image using the code that you pointed to. It cannot find image.png. However, when I tried to find another image which I copied and pasted previously, it did find an image which was there before starting netbeans and scanning classpaths, etc. But in short I dont think it can find dynamically created images - unless I need to add some code.

renanse - I have not programmed signifcantly in Java for over year and a half so forgive my ignorance here - Maze.java and HelloTerrain.java are two different classes. Maze.java creates mazes and I saved it using Buffered reader object and assigning graphics object to it and then using write method of Image IO class e.g. as below. How do I pass 'bufferedImage' to complexTerrain method of HelloTerrain?


BufferedImage bufferedImage = new BufferedImage ( width, height,BufferedImage.TYPE_INT_RGB );


      g= bufferedImage.createGraphics();


      for (x = 1; x < 19; ++x)

          for (y = 1; y < 19; ++y)


              if ((maze[x][y] & 1) != 0) /
This cell has a top wall /

                  g.drawLine (x * 10, y * 10, x * 10 + 10, y * 10);

              if ((maze[x][y] & 2) != 0) /
This cell has a bottom wall /

                  g.drawLine (x * 10, y * 10 + 10, x * 10 + 10, y * 10 + 10);

              if ((maze[x][y] & 4) != 0) /
This cell has a left wall /

                  g.drawLine (x * 10, y * 10, x * 10, y * 10 + 10);

              if ((maze[x][y] & 8) != 0) /
This cell has a right wall */

                  g.drawLine (x * 10 + 10, y * 10, x * 10 + 10, y * 10 + 10);






            ImageIO.write( bufferedImage, "png", new File ("Z:/My Work/Year 4/JMonkeyEngine/jme/src/jmetest/data/texture/image.png") );


        catch (IOException e1)









Kind Regards,


I would look at calling Maze from Hello and having it return from one of its methods the BufferedImage.  If that doesn't make a lot of sense, I'd suggest taking a little time to get up to speed in Java first as jME will probably be a tad confusing otherwise.

I just thought of something… since the way you are lading your image should work with dynamically created images, there is the chance that your classpath is actually looking for resources in the JAR file, instead of in your fileystem… Try to check if the bundled images still load when you (re)move them from the directory or not. If they do, then they are being loaded from the jars instead.

You could try an absolute path - e.g. C:/image.png (after copying the image, and assuming you are using Windows with a drive letter of C), and that might work.

Thank you very much all - appreciate all the help…

renanse - I did think of that previously and tried it but the method is paint of graphics which cannot be override - although I can rewrite the class in a different way and it should be feasible - I'll postpone this part till next week though as I am knee deep in documentation for this week. Also, while in sarcastic tone, I appreciate your point that it will be good for me to get to upto speed with Java so things go smoothly - any websites or quick tutorials you would like to recommend?

TheFearow - I tried the absolute path first but it kept giving me null pointers so then I went to the relative path and now trying to figure out dynamic loading(if there is such a thing)…

duenez - I agree that could be the case… May be there might have been something wrong with the download and installation… Finally deleted everything, downloaded jME, cleaned and build everything and tried it again and it worked! Its giving me correct url in the code, no null pointers!

Again thank you very much.

Kind Regards,


Glad I could help!, I will take the liberty to post the link to the JavaTutorial instead of Renanse, just for the sake of it  :evil:


hawk2k8 said:
Also, while in sarcastic tone, I appreciate your point that it will be good for me to get to upto speed with Java so things go smoothly - any websites or quick tutorials you would like to recommend?

I was actually sincere... not sarcastic.  It's difficult to do well with a complex API if you are not up to speed yet on the underlying language.  :|