Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
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 3636 times)
0 Members and 1 Guest are viewing this topic.
Offline Shigsy

Junior Devvie




Im great, your not!


« Posted 2005-02-15 19: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 Devvie




where I should sign ?


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

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

Junior Devvie




Im great, your not!


« Reply #2 - Posted 2005-02-16 08: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!
Legends of Yore - The Casual Retro Roguelike
Offline tom
« Reply #3 - Posted 2005-02-16 13: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 Devvie




Im great, your not!


« Reply #4 - Posted 2005-02-16 14: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 01: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 Devvie




Im great, your not!


« Reply #6 - Posted 2005-02-17 15: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 20: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.

 

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

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (31 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (60 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 01:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 13:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 13:36:02

List of Learning Resources
by Longor1996
2014-08-16 01:40:00

List of Learning Resources
by SilverTiger
2014-08-05 10:33:27

Resources for WIP games
by CogWheelz
2014-08-01 07:20:17

Resources for WIP games
by CogWheelz
2014-08-01 07:19:50

List of Learning Resources
by SilverTiger
2014-07-31 07:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
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!