Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

A buddy and I have been programming a code game over the last 2 weeks, it is intended to be a multiplayer game with the ability to have 2 players play against each other.

I am running into an issue when I try and implement the server-client relationship. My client crashes when it tries to receive information from the server.

The main issue is in the GameplayState.java file. the getTile() method makes the client crash, so I am assuming the issue lies with how I'm handling the information being sent from the server.

Is there a way to fix this and prevent the crashing? would there be a better way to achieve what I am trying to do?

We are using the lwjgl, Slick2d libs

Specifically its the call: serverIn.readLine(); that makes crap go crazy.

Finally receive an error

Wed Jun 13 21:03:58 ADT 2012 ERROR:For input string: ",B6,W5,B2,W7,B,B,W,W" java.lang.NumberFormatException: For input string: ",B6,W5,B2,W7,B,B,W,W" at java.lang.NumberFormatException.forInputString(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at java.lang.Integer.parseInt(Unknown Source) at GameplayState.getTile(GameplayState.java:168) at GameplayState.update(GameplayState.java:124) at org.newdawn.slick.state.StateBasedGame.update(StateBasedGame.java:268) at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:657) at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408) at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318) at CodaClient.main(CodaClient.java:34) Wed Jun 13 21:03:58 ADT 2012 ERROR:Game.update() failure - check the game code. org.newdawn.slick.SlickException: Game.update() failure - check the game code. at org.newdawn.slick.GameContainer.updateAndRender(GameContainer.java:663) at org.newdawn.slick.AppGameContainer.gameLoop(AppGameContainer.java:408) at org.newdawn.slick.AppGameContainer.start(AppGameContainer.java:318) at CodaClient.main(CodaClient.java:34)

Relevant server source code below:

Because I'm getting flak for putting all the code in, for ease, I will bold the statements that say what files the issue arises in

Client Code

Server Code

share|improve this question
3  
Yikes. No one wants to look through all that source code! Put a try catch block around your networking code and see what pops out. Or use the debugger and step through some simple cases. –  Byte56 Jun 13 '12 at 23:28
    
haha ya, if I knew what the issue was I would be more precise, I have a try block around the issue areas and network code and nothing seems to come up, and I've been trying to find it with break points, to no avail –  Latency Jun 13 '12 at 23:31
2  
Surely you can be more precise than all of your source code? I mean, do you really think it's in the tileList or tile classes? Does this crash immediately on startup or after some action? You say it's when the client tries to read from the server, so you must at least be able to narrow it down to one or two classes? Perhaps two functions? What's the error message being produced? –  Byte56 Jun 13 '12 at 23:38
1  
-1 for code dump. Sorry. :( Maybe you could try Code Review? –  Wackidev Jun 13 '12 at 23:41
1  
Code review isn't for a code dump either. It's for a review of working snippets of code, not to find bugs or to go over architecture of an entire system. –  Tetrad Jun 13 '12 at 23:46
show 1 more comment

1 Answer

up vote 4 down vote accepted

Error stacks go from the most recent call to the least recent, so the most recent line is what happened, and all the rest are what it happened within, what method that was called from, what method that was called from, and so on.

Wed Jun 13 21:03:58 ADT 2012 ERROR:For input string: ",B6,W5,B2,W7,B,B,W,W"
java.lang.NumberFormatException: For input string: ",B6,W5,B2,W7,B,B,W,W"
    at java.lang.NumberFormatException.forInputString(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at java.lang.Integer.parseInt(Unknown Source)
    at GameplayState.getTile(GameplayState.java:168)
    at GameplayState.update(GameplayState.java:124)

Or to break it down...

// this is the exception time & message
Wed Jun 13 21:03:58 ADT 2012 ERROR:For input string: ",B6,W5,B2,W7,B,B,W,W"
// this is the exception itself. It's a NumberFormatException, meaning the number
// was of an invalid format. Which is obvious, since the string here isn't a number.
java.lang.NumberFormatException: For input string: ",B6,W5,B2,W7,B,B,W,W"
// the NumberFormatException was created within this method (obviously a helper for
// creating NumberFormatExceptions)
    at java.lang.NumberFormatException.forInputString(Unknown Source)
// this method ^ was called by the following method, which was given an invalid input
    at java.lang.Integer.parseInt(Unknown Source)
// ... which was in turn called by this one ...
    at java.lang.Integer.parseInt(Unknown Source)
// the above parseInt was called within YOUR getTile method, leading to the exception
    at GameplayState.getTile(GameplayState.java:168)
// which was called within your normal game update.
    at GameplayState.update(GameplayState.java:124)
// and so on.

You called parseInt with an invalid input it obviously can't turn into a number (because it isn't a number), so of course it's going to throw an exception saying it can't do that - that's exactly what's supposed to happen.

Let's see what you're doing in getTile() which is the method that parseInt is called within.

private void getTile() {

    //System.out.println(serverResponse);
    if(serverResponse != "")
        currentTile = new Tile(Integer.parseInt(serverResponse.substring(1)), serverResponse.charAt(0));
}

String.substring(1) returns the string from position 1 (i.e. after the first character) to its end, which in this case ends up as something like ,B6,W5,B2,W7,B,B,W,W. Here's how you stop getting your error: don't do that.

If you meant to just take the first digit, use:

  • serverResponse.substring(0, 1), or
  • serverResponse.charAt(0), or
  • serverResponse[0] (this would be the neatest way to do that in Java, if it's allowed).

If you meant to do something else, well, it's doing this currently. Code it to do something else instead. You have a logic error which means it's up to you to figure out how to get it to do what you want it to do.

Your lesson:

  • Read the stack trace
  • Debug your code
  • Remember your logic could be the problem (it is not perfect)
share|improve this answer
    
That fixed the issue, I was receiving the wrong string (or actually never asking for it from the server) so the line was throwing an error. Thank you. –  Latency Jun 14 '12 at 0:44
1  
+1 for the answer and lesson. @Latency debugging is as important a skill as writing code in the first place =) –  Patrick Hughes Jun 14 '12 at 6:35
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.