Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

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

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)
like image 726
Suhail Gupta Avatar asked Nov 06 '13 07:11

Suhail Gupta


People also ask

Does Serializable interface belongs to Java IO package?

The Serializable interface is present in java.io package. It is a marker interface. A Marker Interface does not have any methods and fields. Thus classes implementing it do not have to implement any methods.

What happens if we implement Serializable interface in Java?

Serialization in Java allows us to convert an Object to stream that we can send over the network or save it as file or store in DB for later usage. Deserialization is the process of converting Object stream to actual Java Object to be used in our program.

How do I prevent some data from getting serialized?

You can prevent member variables from being serialized by marking them with the NonSerialized attribute as follows. If possible, make an object that could contain security-sensitive data nonserializable. If the object must be serialized, apply the NonSerialized attribute to specific fields that store sensitive data.


1 Answers

Your object contains member variables which are themselves not serializable (an instance of 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.

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. 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.

like image 162
Matthias Avatar answered Sep 22 '22 18:09

Matthias