[States] [Networking] [slick2d] Game state is being initialized when not wanted

Seems like my game state is being initialized even though I have it commented out in the initStatesList(…) is there a reason for that? I have it initializing network code would that affect the initializing?

the exact error im getting is:

[quote]Unable to get I/O connection to: localhost on port: 1967
Exception in thread “main” java.lang.NullPointerException
at GameplayState.init(GameplayState.java:136)
at org.newdawn.slick.state.StateBasedGame.init(StateBasedGame.java:171)
at org.newdawn.slick.AppGameContainer.setup(AppGameContainer.java:390)
at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:314)
at CodaClient.main(CodaClient.java:33)
[/quote]
but the only connection attempt to localhost is in the game state

Heres my initStatesList:


public void initStatesList(GameContainer gameContainer) throws SlickException {
        this.getState(MAINMENUSTATE).init(gameContainer, this);
        //this.getState(GAMEPLAYSTATE).init(gameContainer, this);
        this.getState(VICTORYSTATE).init(gameContainer, this);
        this.getState(OPTIONSSTATE).init(gameContainer, this);
}

and the states added to the state list:


super("Title");

this.addState(new MainMenuState(MAINMENUSTATE));
this.addState(new GameplayState(GAMEPLAYSTATE));
this.addState(new VictoryState(VICTORYSTATE));
this.addState(new OptionsState(OPTIONSSTATE));

this.enterState(MAINMENUSTATE);

Complete Source Code:
CodaClient.java: http://pastebin.com/SvGnbVXr
MainMenuState.java: http://pastebin.com/W8bBDbn6
GameplayState.java: http://pastebin.com/rXm0kkik
OptionsState.java: http://pastebin.com/wStTBq0L
VictoryState.java: http://pastebin.com/gvUpGJf9

Based on slickBox Tutorial: http://slick.cokeandcode.com/wiki/doku.php?id=02_-_slickblocks

Edit: couldn’t get the JGO files to work so I just used pastebin

Where is the code?

sorry forgot to put it in, it’s there now. Is there anything else that I should include to make this easier? like the gameState init()?

The exception is pretty clear. Look at GameplayState.java line 136. I can’t see what it is since it’s not full source*.

*) if you want to post more source and they’re huge, use JGO’s pastebin.

I’ll post the full source, but the issue is that it’s even running that code, I don’t need that code until it has the data that I declare earlier in the program. Basically in the menus I get the user to login, then it sends those credentials to the server in the GameState. But on Launch the GameState init() is being run when I dont want to call it until I need it.

Ill post full source code once I copy it to the bin, but would there be a better way to approach something like this?

How about override init() method so it’ll only execute when a boolean is true?

Full source is uploaded, and I could try that, but that sounds like a messy workaround, considering that the initStatesList() is meant as a list to declare what to init (or so I thought) and basically SHOULD do the same thing. I probably just messed up something in my declarations that I didn’t notice cause I am quite new to the Slick2D lib

You shouldn’t put it in GamePlay’s init() method. All code inside that method should be ready to execute on earlier. Your login method should stay on MainMenu state, and if success you can change to game state. The flow is kinda like this:

  1. All states added and inited, done before user see the menu.
  2. All states are ready and sit to wait. MainMenu comes first.
  3. User try to log in. The process is done before changing state. So if success, change it Game state otherwise keep on there.

Alright ill give that a shot, thank you

Ugh… :cranky: The tutorial is wrong (it’s been updated now).

You should add states in initStatesList. After initStatesList is called, StateBasedGame will initialize all of them for you (so you shouldn’t call state.init yourself!! otherwise they will be initialized twice). This means all of your states will be “initialized” at the beginning of your game.

If you don’t want this, you can either add them to your game and manually initialize them at some point after initStatesList… Or (preferred method) you can handle the initialization differently: subclass GameState and StateBasedGame and set up your own “activated” and “deactivated” methods that define when the state is first enetered, and then left. This way you can lazily load and destroy resources whenever the user enters/leaves states.

So, proper state initialization looks like this:

public void initStatesList(GameContainer gameContainer) throws SlickException {
        this.addState(new MainMenuState(MAINMENUSTATE));
        this.addState(new GameplayState(GAMEPLAYSTATE));
}

And your constructor shouldn’t have anything related to states:

super("Title");

By default, the first time you add a state in initStatesList it will be ‘entered’ (in this case main menu). So you could still use enterState after adding states, if you want to start with a different state.

If I try it that way it throws an error about not having a state with an ID of 0, so they are not initialized at all (I’m assuming), even if I leave the


this.getState(MAINMENUSTATE).init(gameContainer, this);
this.getState(GAMEPLAYSTATE).init(gameContainer, this);

sections in, once the .addstate() code is moved it doesnt work.

And Rebirth, the change to the way I handle the connection info works wonderfully, thank you. But I needed to keep it in the GameState, But I adjusted based on what you said

If I try it that way it throws an error about not having a state with an ID of 0, so they are not initialized at all (I'm assuming)

Post the exception please. This will happen if you try entering a state before you’ve added it via addState.

I can assure you that calling init() manually like you are doing is not the correct usage… :cranky: And it will lead to the state being initialized twice. (i.e. all images/sound will be loaded more times than necessary)

Exception in thread “main” java.lang.RuntimeException: No game state registered with the ID: 0
at org.newdawn.slick.state.StateBasedGame.enterState(StateBasedGame.java:153)
at org.newdawn.slick.state.StateBasedGame.enterState(StateBasedGame.java:131)
at CodaClient.(CodaClient.java:22)
at CodaClient.main(CodaClient.java:27)

Are you calling enterState before adding the states to your game? :persecutioncomplex:

I totally am, I’ll adjust it. Sorry I’ve been programming this non-stop since Monday, think I’m getting sloppy.

Ya that fixed it, thank you.

Great that work for you.