writeObject(), NotSerializableException

Hi, im getting this error…

Exception: java.io.NotSerializableException:

…when i try to use the “writeObject(obj)” method. As far as i can make out from what ive read on the net, i need to do soemthing to do with Serialization for the readObject() and writeObject methods but im not sure what.

i think the problem is the fact that im using the readObject() and writeObject() methods in the wrong way. I was trying to use them to send data (a Sprite object) but im starting to think thats not what they are for, or else im using them arseways.

What methods can i use to send an object as a packet between the client and server? Any simple code example?
thanks.

the object you want to put in your stream need to implement the dummy interface java.io.Serializable

yeah thats what i thought so i made my Sprite class (its an object of the Sprite class that im trying to send) implement Serializable but then the error changes from

NotSerializableException: Sprite

to

NotSerializableException: BufferedImage

The Sprite class uses an image to create the player Sprite. So, any ideas on how to fix this?
Thanks.

Serialization works by recursivly traverse all the objects referenced by the object you try to write. They all have to implement Serializable. Although I think you can overide “private void writeObject(java.io.ObjectOutputStream out)” and parse the objects yourself. Look at the java.io.Serializable javadoc.

Since you post in the network section I assume you are trying to send the object over the net. Then you have to carefully design the objects you want to send. Try to only use primitive types, and remove all objects. As an example you do not want to have a reference to a BufferedImage in your sprite. It would cause the whole image to be sent every time you send your sprite. Instead keep a int id that looks to image up from a table.

You may also consider ditching serialization and parse your object manually using DataInputStream and DataOutputStream.

Its a LAN game, not Internet. Heres what im trying to do, maybe you can suggest a better way.

When i move my player (an object of the Sprite class) i want to update all the clients screens so i was creating a copy of the Sprite object and trying to send that. The reason i dont simply send the players new x,y coords is because thats not enough information i.e. it doesnt say what direction the player is facing (he can walk in 8 different directions).
Can you suggest a way for me to do this? Maybe a bit of sample code? :wink:
Thanks.

[quote]Its a LAN game, not Internet.
[/quote]
Even if it’s a lan game you don’t want to send the sprite image everytime time you send a packet :slight_smile:

[quote] The reason i dont simply send the players new x,y coords is because thats not enough information i.e. it doesnt say what direction the player is facing (he can walk in 8 different directions).
[/quote]
Then just send all the information that is needed.

[quote]Can you suggest a way for me to do this? Maybe a bit of sample code?
[/quote]
Ok then:


// socketOut is the socket output stream
DataOutputStream dataOut = new DataOutputStream(socketOut);
dataOut.writeInt(posx);
dataOut.writeInt(posy);
dataOut.writeFloat(direction);
// todo: write all other information that needs to be sent

Look at the javadoc for java.io.DataOutput java.io.DataInput, and all the other classes in java.io for that mather.

Is this code for TCP cause im using UDP. Im using Datagram packets.

Yes, socketOut could be the output stream used with tcp. With udp you would write it to a ByteArrayOutputStream. When you were finished you call getBytes() and send the byte array with udp.