Advertisement Jump to content
Sign in to follow this  

Java Null Pointer Exception

This topic is 2635 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts


I'm currently working a a 2D dungeon crawler game. I have a tile based system with a Tile class. This contains a whole heap of public static Tile. The problem is, when I try to access these from anywhere, I get a Null pointer Exception. I don't how i get this though since I quite clearly instatiate the variables. Heres the code

public static Tile backgroundRock;
public static Tile bars;
public static Tile dirt;
public static Tile grass;
public static Tile grill;
public static Tile platform;
public static Tile rock;
public static Tile shaft;
public static Tile table;
public static Tile water;

bars = new TileBars(0, 0, 0x404040);
dirt = new TileDirt(1, 1, 0x7F3300);
grass = new TileGrass(2, 2, 0x267F00);
grill = new TileGrill(3, 3, 0x3F2F2F);
rock = new TileRock(4, 4, 0x808080);
shaft = new TileShaft(5, 5, 0xFFD800);
table = new TileTable(6, 6, 0xFF4300);
water = new TileWater(7, 7, 0x0094FF);
platform = new TilePlatform(8, 8, 0x423300);
backgroundRock = new TileBackRock(9, 9, 0xFFFCF4);

Please help, is there something I'm missing...

Share this post

Link to post
Share on other sites
The code you posted seems fine. A quick test (with a dummy class of course) runs just fine. Can you verify with a debugger that the static variables are actually null?

Share this post

Link to post
Share on other sites
There is a possibility that the static initializer isn't being run but I think it's highly unlikely.

Try putting a system out in the static initializer. See if it prints.

If it's not being run then you might need to call a method on the class before you use it.

Share this post

Link to post
Share on other sites
Well, the values are not final, so it is possible that you are assigning to them somewhere. I imagine you would quickly have found this were it the problem.

Are you sure there are no exceptions/throwables being swallowed? An exception in a static initialiser can end up being thrown from the first place the class is referenced.

Try adding a breakpoint to the static initialiser to ensure it completes successfully, and/or adding a try...catch block in the static initialiser to print any exception occurring (allow it to propagate though).

Share this post

Link to post
Share on other sites
On another note, why have you created subclasses of Tile, and then supply info that seems to be constant for those subclasses to the constructors? This is error prone.

Usually a cleaner solution is to make a single Tile class, and read properties of different Tile types from a file. If that's overkill for your game, at the very least don't require the user of your classes to supply those constant values to the constructors.

Of course, if these values aren't constant, you can disregard this post altogether :wink:

You can also assign values to your static variables where you declare them, so you can make them final:

static final Tile bars = new TileBars(0, 0, 0x404040);

Share this post

Link to post
Share on other sites
Static blocks are called when the class is loaded by ClassLoader, so it's impossible the static block hasn't been called. Can you write the line where it throws the exception or the way you are accessing these objects?

Share this post

Link to post
Share on other sites
Don't use static block.
use a static method and invoke it at startup.
This is more safe and clear, because you exactly know when the variables get initialized..

Static block may have weird behavior in such situation especially on some mobile device (if you are developing mobile game).

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!