简体   繁体   English

我收到Java.io.notserializableException错误

[英]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. 尝试在扩展jpanel的类中实现向服务器中写入对象并实现可serializable之后,出现java.io.notserializableException错误,但是如果扩展jframe. ,效果很好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: JPanel(Java 2 Platform SE v1.4.2)可以看到

Warning: Serialized objects of this class will not be compatible with future Swing releases. 警告:此类的序列化对象将与以后的Swing版本不兼容。 The current serialization support is appropriate for short term storage or RMI between applications running the same version of Swing. 当前的序列化支持适用于运行相同版本的Swing的应用程序之间的短期存储或RMI。 As of 1.4, support for long term storage of all JavaBeans TM has been added to the java.beans package. 从1.4版开始,已将对所有JavaBeans TM的长期存储的支持添加到java.beans包中。 Please see XMLEncoder . 请参阅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 . 您对JPanel实现( myobject实例)具有对某个其他对象的引用(例如,私有字段),该引用未实现Serializable And it's that other object, that is causing the exception. 正是那个其他对象导致了异常。

As @Xeon said, provide impl for myobject. 正如@Xeon所说,为myobject提供impl。 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. 正如您所说的,对象类即将出现异常,这仅意味着您的类或任何类成员,即字段(这是一个对象,不是Premitive类型)未实现Serializable接口。 Also how do you create myobject there? 另外,您如何在其中创建myobject? just directly Object myobject = ... 只是直接对象myobject = ...
or myobject = .... ? 或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 ! 对象类提供方法,但不实现该可序列化的接口,否则默认情况下Java中的每个对象都可以被序列化!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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