简体   繁体   中英

I'm getting Java.io.notserializableException error

I'm getting java.io.notserializableException error after trying to write an object to a server in a class extending jpanel and implementing serializable but it worked well if extending jframe.

Here is the code:

//to send to server 
objectoutputstream out = new objectoutputstream(socket.getoutputstream()); 
out.writeobject(myobject); 

//to receive in server 
objectinputstream in = new objectinputstream(socket.getinputstream()); 
in.readobject();

Thanks for any help.

From JPanel (Java 2 Platform SE v1.4.2) , it says:

Warning: Serialized objects of this class will not be compatible with future Swing releases. The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. As of 1.4, support for long term storage of all JavaBeans TM has been added to the java.beans package. Please see XMLEncoder .

Your implementation of the JPanel (the myobject instance) has a reference (a private field, for example) to some other object, that is does not implement Serializable . And it's that other object, that is causing the exception.

As @Xeon said, provide impl for myobject. As you said that exception is coming on Object class, that simply means either your class or any class-member ie field (which is an object, not a premitive type) is not implementing Serializable interface. Also how do you create myobject there? just directly Object myobject = ...
or myobject = .... ?

Just ensure that everyone implements serializable.

Object class provides methods but doesn't implements that serializable interface, otherwise every object in Java would have been serializable by default !

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM