I have recently been programming a platforming game in java, and I have run into a problem when I tried to use a separate class to store my map (tiled). I then tried to fix it by moving the map into the main class, and it didn't help.
This is the code that is causing the problem (When the world.map1.render(0,0) is removed, no problem exists) - I know that I should be using getters and setters. I'm just find them a pain.
public void render(GameContainer arg0, Graphics arg1) throws SlickException {
world.map1.render(0, 0);
}
this is the main method
public static void main(String[] args) throws SlickException{
AppGameContainer app = new AppGameContainer(new Game());
app.setDisplayMode(864, 480, false);
app.setVSync(true);
app.start();
playerSheet = new SpriteSheet("resources/images/link3goldstudmod.png", 64, 64);
player = new Player(playerSheet);
world = new World();
}
and this is the World class
import org.newdawn.slick.SlickException;
import org.newdawn.slick.tiled.TiledMap;
public class World {
TiledMap map1;
World() throws SlickException{
map1 = new TiledMap("resources/maps/map1.tmx");
}
}
EDIT: I forgot to post the problem/stack crap/whatever. How silly of me.
Mon Feb 18 16:33:58 MST 2013 INFO:Slick Build #264
Mon Feb 18 16:33:58 MST 2013 INFO:LWJGL Version: 2.8.5
Mon Feb 18 16:33:58 MST 2013 INFO:OriginalDisplayMode: 1920 x 1080 x 32 @60Hz
Mon Feb 18 16:33:58 MST 2013 INFO:TargetDisplayMode: 864 x 480 x 0 @0Hz
Mon Feb 18 16:33:58 MST 2013 INFO:Starting display 864x480
Mon Feb 18 16:33:58 MST 2013 INFO:Use Java PNG Loader = true
WARNING: Found unknown Windows version: Windows 8
Attempting to use default windows plug-in.
Loading: net.java.games.input.DirectAndRawInputEnvironmentPlugin
Mon Feb 18 16:33:59 MST 2013 INFO:Found 3 controllers
Mon Feb 18 16:33:59 MST 2013 INFO:0 : USB Receiver
Mon Feb 18 16:33:59 MST 2013 INFO:1 : USB Receiver
Mon Feb 18 16:33:59 MST 2013 INFO:2 : Logitech Speaker
Mon Feb 18 16:33:59 MST 2013 ERROR:null
java.lang.NullPointerException
at zeldaplatform.Game.render(Game.java:36)
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:703)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
at zeldaplatform.Game.main(Game.java:27)
Mon Feb 18 16:33:59 MST 2013 ERROR:Game.render() failure - check the game code.
org.newdawn.slick.SlickException: Game.render() failure - check the game code.
at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:706)
at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:456)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:361)
at zeldaplatform.Game.main(Game.java:27)
It's not much of an error, because it compiles just fine, and then the console gives me this. The window shows up for a split second, and then disappears.
It's a NullPointerException
. There is exactly one cause for an NPE; the thing you are attempting to use is not instantiated and is null
. In this case, that's either world
in your Game
class or map1
in your World
class.
Considering you instantiate a new Game
at the start of your Game.main()
, start it, and then proceed to assign a new World
to a static
field of said class (you don't post your Game
class, but that's the only way you could do that from main()
and the stack trace shows that it's Game.main()
), it's the former.
AppGameContainer
(app
) is being fired up with start()
and then that thread proceeds to call that instance of Game
's render()
method before you have instantiated and assigned your World
to the static field.
Don't use static
fields like that; it's really not what they are for. You should be passing those instances (playerSheet
, player
, and world
) to a constructor of Game
and assigning them to private instance variables.
...
Game myGame = new Game(playerSheet, player, world);
AppGameContainer app = new AppGameContainer(myGame);
...