Java-Gaming.org
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
Featured games (78)
games approved by the League of Dukes
Games in Showcase (408)
games submitted by our members
Games in WIP (293)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  writeObject(), NotSerializableException  (Read 2585 times)
0 Members and 1 Guest are viewing this topic.
Offline Shigsy

Junior Member




Im great, your not!


« Posted 2005-02-15 20:42:47 »

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.

I can resist everything except temptation!
Offline vrm

Junior Member




where I should sign ?


« Reply #1 - Posted 2005-02-16 04:46:14 »

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

Junior Member




Im great, your not!


« Reply #2 - Posted 2005-02-16 09:21:48 »

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.

I can resist everything except temptation!
Games published by our own members! Check 'em out!
Try the Free Demo of Titan Attacks
Offline tom
« Reply #3 - Posted 2005-02-16 14:53:04 »

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.

Offline Shigsy

Junior Member




Im great, your not!


« Reply #4 - Posted 2005-02-16 15:40:52 »

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.

I can resist everything except temptation!
Offline tom
« Reply #5 - Posted 2005-02-17 02:47:14 »

Quote
Its a LAN game, not Internet.

Even if it's a lan game you don't want to send the sprite image everytime time you send a packet Smiley

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).

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?

Ok then:
1  
2  
3  
4  
5  
6  
// 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.

Offline Shigsy

Junior Member




Im great, your not!


« Reply #6 - Posted 2005-02-17 16:06:39 »

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

I can resist everything except temptation!
Offline tom
« Reply #7 - Posted 2005-02-17 21:00:21 »

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.

Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars!
 
Play Revenge of the Titans! The situation is critical. We need fancy commanders to defend Earth, the moon, Mars and Titan!

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

The invasion has landed! On Mars! And you're there to beat 'em!
cubemaster21 (102 views)
2013-05-17 21:29:12

alaslipknot (112 views)
2013-05-16 21:24:48

gouessej (140 views)
2013-05-16 00:53:38

gouessej (136 views)
2013-05-16 00:17:58

theagentd (147 views)
2013-05-15 15:01:13

theagentd (133 views)
2013-05-15 15:00:54

StreetDoggy (176 views)
2013-05-14 15:56:26

kutucuk (198 views)
2013-05-12 17:10:36

kutucuk (200 views)
2013-05-12 15:36:09

UnluckyDevil (206 views)
2013-05-12 05:09:57
Complex number cookbook
by Roquen
2013-04-24 12:47:31

2D Dynamic Lighting
by Oskuro
2013-04-17 16:46:12

2D Dynamic Lighting
by Oskuro
2013-04-17 16:45:57

2D Dynamic Lighting
by Oskuro
2013-04-17 16:23:20

Noise (bandpassed white)
by Roquen
2013-04-05 17:36:01

Noise (bandpassed white)
by Roquen
2013-04-03 16:17:38

Java Data structures
by Roquen
2013-03-29 13:21:12

Topic Request
by kutucuk
2013-03-22 21:42:01
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!
Page created in 0.129 seconds with 21 queries.