简体   繁体   English

当类实现Serializable接口时,为什么会得到java.io.NotSerializableException?

[英]Why am I getting java.io.NotSerializableException when the class implements Serializable interface?

As I try to serialize an object with a call , stashCon.stash() I get java.io.NotSerializableException even when the class StashCon implements Serializable interface. 当我尝试使用调用serialize对象时, stashCon.stash()即使在StashCon类实现Serializable接口时也会得到java.io.NotSerializableException

What could be the reason for this ? 这可能是什么原因?

public boolean connect(String username,String password) {
    try {
        Openfire.connection.connect();
        Openfire.connection.login(username,password);
        stashCon = new StashCon(Openfire.connection);
        stashCon.stash(); // CALL THAT ATTEMPTS TO SERIALIZE THE OBJECT
    }catch(Exception exc){
        exc.printStackTrace();
        return false;
    }
    return true;
}

Following method is of the class StashCon 以下方法属于StashCon

public void stash() {
    try {
        FileOutputStream outputStream = new FileOutputStream(new File(Constants.BLAB_CONNECTION_FILE));
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(this); // LINE 33
        objectOutputStream.close();
        outputStream.close();
    }catch(Exception exc) {
        exc.printStackTrace();
    } 
}

Exception 例外

java.io.NotSerializableException: org.jivesoftware.smack.XMPPConnection
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1509)
at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1474)
at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1392)
at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1150)
at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
at blab.StashCon.stash(StashCon.java:33)
at blab.Openfire.connect(Openfire.java:27)
at blab.ext.gui.SignIn$4.run(SignIn.java:214)
at java.lang.Thread.run(Thread.java:619)

Your object contains member variables which are themselves not serializable (an instance of org.jivesoftware.smack.XMPPConnection ). 您的对象包含本身不可序列化的成员变量( org.jivesoftware.smack.XMPPConnection的实例)。

If you really want to serialize your object, you'll have to do something about that member variable. 如果你真的想要序列化你的对象,你将不得不对该成员变量做些什么。 One option would be to declare that variable as transient so that it is not serialized. 一种选择是将该变量声明为transient以便它不被序列化。

On deserialization however, you'll have to handle that member (like reastablishing the connection). 但是,在反序列化时,您必须处理该成员(例如重新建立连接)。 For this you could define the method readObject which is called during deserialization. 为此,您可以定义在反序列化期间调用的方法readObject In there you can (and probably should) initialize all transient member variables to set your object to a good state. 在那里,您可以(并且可能应该)初始化所有瞬态成员变量,以将您的对象设置为良好状态。

Here is also a good question discussing serialization. 也是一个讨论序列化的好问题。

XMPPConnection is coupled to physical resources (TCP sockets) on the machine in which it resides, therefore it cannot be made serializable. XMPPConnection与其所在机器上的物理资源(TCP套接字)耦合,因此无法进行序列化。 It is also has an identitiy associated with the connection to the server which cannot be duplicated, since a full JID can only be connected to the same server once, a second connection would force the other one to get disconnected. 它还具有与服务器的连接相关联的标识,该连接无法复制,因为完整的JID只能连接到同一服务器一次,第二个连接会强制另一个连接断开连接。

The connection should not be put into the session, you have to manage it outside of that scope. 不应将连接放入会话中,您必须在该范围之外进行管理。

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

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