-3

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.

a cut
  • 51
  • 7
  • 1
    `an error` How do you expect us to help if you don't tell us what error? – tckmn Feb 18 '13 at 23:37
  • 1
    It appears you don't understand Objects, variables, or scoping. You may want to start with the Java tutorials from Oracle or a good beginner's book on Java. – Brian Roach Feb 18 '13 at 23:39
  • @BrianRoach What makes you think that? I don't know what would, because I have read through the javadocs, and I do know what objects are and what they should be used for. I just wrote this question in a hurry. If I didn't know what variables are, I wouldn't have started slick. Yes, I can write pong games (without copy/paste). But I'm not very good with the slick engine. It gives me trouble. And hard to find tutorials, and stuff for. – a cut Feb 18 '13 at 23:58

1 Answers1

1

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);
...
Brian Roach
  • 76,169
  • 12
  • 136
  • 161
  • I removed all of the static fields and now I get an opengl "no opengl found in current thread" error. And supposedly the only fix is to add the stuff from the world class to the game class. But last time I checked you are supposed to keep things seperated in classes... unless i do a private class. I'll try it and see what happens – a cut Feb 19 '13 at 00:24
  • 1
    Without seeing/digging into the rest of your code ... that makes sense. That `render()` method you posted from your `Game` class is being passed a `Graphics` context which you're not doing anything with; you need that context to render stuff (and you never want to store a `Graphics` context and reuse it). – Brian Roach Feb 19 '13 at 00:30
  • I saw your comment and took a closer look at some of the sample code for this, and I realized that slick is very picky. Picky to the point where it wont let you set variables or objects until you get to the init() function (before game loop) and.. i moved the stuff i had in Game() to init and currently the program is running fine. It shows my map. (Good enough for me!) And, thx. Very much. Although, the sample code I was looking at didn't do anything with the Graphics context, either. – a cut Feb 19 '13 at 00:50