Java Null Pointer Exception

Started by
5 comments, last by wqking 12 years, 5 months ago
Hello.

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;

static{
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...
Advertisement
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?
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.
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).
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:

EDIT:
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);
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?
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).

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement