[ J2ME ] NullPointerException

Started by
6 comments, last by trojanman 16 years, 11 months ago
Hi there, I was programming a little 2D engine in java for cell phones and then I came across a problem. The application throws a mysterious NullPointerException when is instantiating all the objects I created for the videogame. The point is this: I couldn't find the error ( after a lot of tries ) inside my code and I'm thinking the problem deals with the huge amount of home made classes instantiated: if I raise down the value I don't get the error anymore! I thought that If I had run out of memory I would have received an OutOfMemory exception; I can't see any other explanation to this problem so.. ..can this NullPointerException be considered as an OutOfMemory exception? ( this question may sound a little bit weird, I know ) Is there any kind of static restriction to the quantity of user classes's instances in J2ME? Thanks in advance!
Advertisement
How many classes are we talking about here that are instantiated? Are they all actually instantiated or just referenced?

Also, have you tried putting in top level try/catch blocks to see if you can catch the error and use printStackTrace() to get where the error is?
Hi! Thank you for the reply!

I have ~200 instances of that class; that's because
a level is composed by 200 tiles.. so I need to keep
trace of every tile's type ( background, platform, water etc. etc. )
to interact in a proper way with the player's character..

import javax.microedition.lcdui.*;import javax.microedition.lcdui.game.*;/** * * class with the informations for every single tile */public class TileBox {        private int xPos; // position of the current tile    private int yPos;    private int type; // type of the tile we are handling on that instance    private Sprite tileSprite; // sprite for rendering operations        /** Creates a new instance of TileBox */    public TileBox( int x, int y, int t, Image img ) {                xPos = x; // store the position        yPos = y; //         type = t; // store the type        tileSprite = new Sprite( Image.createImage( img ) );         tileSprite.setPosition( xPos, yPos );    }        // renders this tile    public void render( Graphics g ) {                tileSprite.paint( g );           }    }


I didn't use a try/catch block but I'm sure the error occurs inside
the for "instancing tiles" cycle.
You shouldn't have problems with what you are trying to do, although I imagine there is a better way to do whatever it is your doing (just a side thought).

I would suggest the try/catch block in various places to narrow down the issue. A NullPointerException should not have anything to do solely on the number of instances of a class you have. My guess is (if you've absolutely narrowed it down to this code) that you are trying to create an image out of a null object.

As a note, why are you doing this: Image.createImage( img ). Why not just create the sprite with the image you already have? Your using excess memory unnecessarily.
Thank you for the advices!

I'll keep on trying with try/catch blocks and
saving resources where it is possible!
( yeah, Image.createImage() in that case is just a waste! )
I caught the exception in the level's render loop!

It's pretty strange because I got all the level perfectly
rendered ( every tile is displayed! ) so I can't figure out why do I have this
NullPointerException..

private void render() {                Graphics g = getGraphics();                 // ..........                        actor.render( g );                        try{                        level.render( g );        }        catch( java.lang.NullPointerException e ) {            System.out.println( "Exception during level's rendering operations" );        }        flushGraphics();    }public void run() {                while( running  ) {                        //...            // GAME'S ROUTINE            cycle();  // logic loop            render(); // call the main render method            //...        }}


I got the exception only three times and then everything runs without any problem!
The level.render() method is continuously called, so why I just get three exceptions?
the object must have been in an invalid state three times. Then something else got processed which make the object valid.
Quote:Original post by Clawer
I caught the exception in the level's render loop!

It's pretty strange because I got all the level perfectly
rendered ( every tile is displayed! ) so I can't figure out why do I have this
NullPointerException..

*** Source Snippet Removed ***

I got the exception only three times and then everything runs without any problem!
The level.render() method is continuously called, so why I just get three exceptions?


1) Have you tried putting the try/catch block in different parts of level::render(). Unfortunately, without more information or more time spent debugging its hard to figure out exactly where the problem lies.

2) Since this happens in the emulator( am I right? ) you could use System.out.println() and view the console, combined with try/catch to figure out where the code fails and which object becomes null. By doing so you should be able to narrow down which object, and then eventually why the object becomes null. Like alnight has said, an object becomes invalid and then something else will validate it later on.

3) This is an observation. Why are you choosing to not use the paint() method from the canvas to dispatch your paint calls? The paint() event is generated for a reason.

This topic is closed to new replies.

Advertisement