Sign in to follow this  
Clawer

[ J2ME ] NullPointerException

Recommended Posts

Clawer    166
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!

Share this post


Link to post
Share on other sites
trojanman    394
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?

Share this post


Link to post
Share on other sites
Clawer    166
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.

Share this post


Link to post
Share on other sites
trojanman    394
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.

Share this post


Link to post
Share on other sites
Clawer    166
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! )

Share this post


Link to post
Share on other sites
Clawer    166
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?

Share this post


Link to post
Share on other sites
trojanman    394
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this