NullPointerException

  ... the Multiplayer Extension for AndEngine.

NullPointerException

Postby need4sid » Sun Apr 17, 2011 10:37 pm

Hi Nicolas,

I'm writing a nice multiplayer game with AndEngine and I've run into a problem with the multiplayer extension.

Example:
I start the server on my Nexus One and the client on my good old G1.
I use the G1, so it receives/reads the server messages and I can play on it.
Sometimes NullPointerException-s start to happen, the game gets stuck.
It seems like it happens inside the extension, so it can't obtain a new message from a MessagePool.
Do you have any idea where does this problem originate? Shouldn't the MessagePool be always able to return a message?

FYI: My server messages are around 190 bytes long and I'm using synchronized code in my screen update callback (where I actually process the message) and also in the message's set/write/read functions. I always synchronize to the message instance at all places, so nothing else will modify the message in any way until I processed the set/read/write/update screen callback on it.
Can this cause the extension to be unable to obtain a new message? If I don't syncronize, it may happen that the MessagePool will consider the message to be reusable before I finish reading data from it - I'm passing the data to the callback by reference, not as ~100-200 separate parameters. To avoid the need of such synchronization, it would be useful if the message had a method like setConsumed() and the callback would receive the message and could call setConsumed() on it right before returning.

(You can see from the logs that it heals after a while.)

Edit: Isn't this because the server is a lot more powerful than the client, so the client just can't process everything that comes in from the server? Anyway, it should just skip the "half of the messages" without getting stuck for seconds in my opinion.

Thanks for any help.
n4s

04-17 20:08:44.510 D/AndEngine( 761): FPS: 12.53 (MIN: 38 ms | MAX: 161 ms)
04-17 20:08:49.500 D/AndEngine( 761): FPS: 15.68 (MIN: 35 ms | MAX: 111 ms)
04-17 20:08:50.590 E/AndEngine( 761): AndEngine
04-17 20:08:50.590 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.590 E/AndEngine( 761): AndEngine
04-17 20:08:50.590 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.590 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.600 E/AndEngine( 761): AndEngine
04-17 20:08:50.600 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.600 E/AndEngine( 761): AndEngine
04-17 20:08:50.600 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.600 E/AndEngine( 761): AndEngine
04-17 20:08:50.600 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.600 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.610 E/AndEngine( 761): AndEngine
04-17 20:08:50.610 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.610 E/AndEngine( 761): AndEngine
04-17 20:08:50.610 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.610 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.620 E/AndEngine( 761): AndEngine
04-17 20:08:50.620 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.620 E/AndEngine( 761): AndEngine
04-17 20:08:50.620 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.620 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.630 E/AndEngine( 761): AndEngine
04-17 20:08:50.630 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.630 E/AndEngine( 761): AndEngine
04-17 20:08:50.630 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.630 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.650 E/AndEngine( 761): AndEngine
04-17 20:08:50.650 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.650 E/AndEngine( 761): AndEngine
04-17 20:08:50.650 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:50.650 E/AndEngine( 761): AndEngine
04-17 20:08:50.650 E/AndEngine( 761): java.lang.NullPointerException
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.util.MessagePool.obtainMessage(MessagePool.java:61)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.MessageReader.readMessage(MessageReader.java:62)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.IServerMessageReader$ServerMessageReader.readMessage(IServerMessag
eReader.java:1)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.client.connector.ServerConnector.read(ServerConnector.java:96)
04-17 20:08:50.650 E/AndEngine( 761): at org.anddev.andengine.extension.multiplayer.protocol.shared.Connection.run(Connection.java:80)
04-17 20:08:54.470 D/AndEngine( 761): FPS: 23.57 (MIN: 34 ms | MAX: 87 ms)
04-17 20:08:59.490 D/AndEngine( 761): FPS: 21.39 (MIN: 33 ms | MAX: 98 ms)
04-17 20:09:04.500 D/AndEngine( 761): FPS: 17.74 (MIN: 34 ms | MAX: 106 ms)
need4sid
 
Posts: 7
Joined: Fri Nov 05, 2010 3:39 pm

Re: NullPointerException

Postby need4sid » Tue Apr 19, 2011 8:03 pm

Nicolas I just rewrote the whole thing in a way that it doesn't block the read/write/callback when the same message is about to used by another thread. It'll just drop what it's getting (any read/write/callback "command") while any other read/write/callback processing is in progress.

I'm still getting the very same NullPointerExceptions on the G1. (same log as above, it starts, it gets stuck for a while in this problem, then it heals)
So, it is not caused by the synchronization.

Can you advise me something?

Thank you.
need4sid
 
Posts: 7
Joined: Fri Nov 05, 2010 3:39 pm

Re: NullPointerException

Postby Nicolas Gramlich » Tue Apr 19, 2011 8:22 pm

Hi,

can you post the code around MessagePool.java:61 and specifically mark line 61.
With the latest revision, line 61 is:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. if(message != null) {
Parsed in 0.031 seconds, using GeSHi 1.0.8.4

which cannot throw a NPE :)

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: NullPointerException

Postby need4sid » Tue Apr 19, 2011 10:43 pm

First of all, thank you for your quick reply.

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.         public M obtainMessage(final short pFlag, final DataInputStream pDataInputStream) throws IOException {
  2.                 final M message = this.mMessageMultiPool.obtainPoolItem(pFlag);
  3.                 message.read(pDataInputStream);
  4.                 return message;
  5.         }
  6.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4


Line 61 is message.read .

Could you please shed some light on the nature of this situation which originally was unhandled? What causes this situation when it gets null instead of a real message? I feel like it shouldn't happen. Should it?
Now, that you check for null, does it also mean that null messages won't cause my app to stall for seconds ocassionally? (it doesn't automatically mean for me that checking for null solves the root cause, that's why I would like to know more if possible)

Another problem Nicolas, sorry about that:
BluetoothSocket is only avalible from API Level 5 and the newest multiplayer extension uses it. (and links against Android 2.2 by default)
Also, it uses Build.VERSION_CODES.ECLAIR_MR1 and Build.VERSION_CODES.FROYO so it doesn't even compile against API Level 5.

The multitouch extension has similar build problems, so I can't compile my project for anything else than 2.2 currently.

I would would like to support everything from 1.6 (level 4). Are you planning to make these extensions link against 1.6?


Thank you!
need4sid
 
Posts: 7
Joined: Fri Nov 05, 2010 3:39 pm

Re: NullPointerException

Postby Nicolas Gramlich » Wed Apr 20, 2011 12:40 am

Hi,

the problem is that the extension (tried to) read the identifying FLAG for a message (in the end a readShort()) and what it actually read does not seem to be a valid flag (--> null retrieved from the MessagePool).

So what probably happened is that the message just before the crash read a byte less or too much than expected.

Btw: The latest source actually throws a proper meaningful Exception in such a case.

Best Regards,
Nicolas
Nicolas Gramlich
Site Admin
 
Posts: 1734
Joined: Mon Jun 07, 2010 6:20 pm
Location: Schriesheim, Germany

Re: NullPointerException

Postby need4sid » Wed Apr 20, 2011 12:50 am

Uh, this last read at the end of the message may interfere with my implementation.
I'm using a ByteBuffer and an underlying byte[] to avoid any GC madness. (with individual input stream reads, it was overdriving the GC, because there were lots of byte[17]-s allocated inside inputstream)

Could you possibly take a look at the corresponding parts of my code?
If you could, can I send it to http://www.andengine.org/blog/wp-content/uploads/2010/06/email.png ?

BTW, could you please also react the extension compiling problem for 1.6? (multiplayer & multitouch can only compile against 2.2)

Thank you.
need4sid
 
Posts: 7
Joined: Fri Nov 05, 2010 3:39 pm


Return to Multiplayer

Who is online

Users browsing this forum: No registered users and 2 guests