当我尝试serialize
调用一个对象时,即使类实现了接口,stashCon.stash()
我也会得到。java.io.NotSerializableException
StashCon
Serializable
这可能是什么原因?
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;
}
以下方法属于该类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();
}
}
例外
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)