Saturday 9 August 2014

NotSerializableException

NotSerializableException thrown when you are trying to serialize an object, but some of the dependencies of the object not implement the Serializable interface.

Example
public class Address {
    String street,city,district,state;
    
    Address(String s, String c, String d, String st){
        street = s;
        city = c;
        district = d;
        state = st;
    }
}

import java.io.Serializable;

public class Employee implements Serializable{
    int id;
    String name;
    Address addr;
    
    Employee(int id, String name, Address addr){
        this.id = id;
        this.name = name;
        this.addr = addr;
    }
}

import java.io.*;

public class SerializeEmployee {
    public static void main(String args[])throws Exception{
        Address addr;
        addr = new Address("Panchayat", "Ongole", "Prakasam", "AP");
        
        FileOutputStream fout = new FileOutputStream("ser.out");
        ObjectOutputStream out = new ObjectOutputStream(fout);
        
        Employee s1 = new Employee(1, "Krishna", addr);
        out.writeObject(s1);
        
    }
}

When you tries to run the above Application, then 'java.io.NotSerializableException' is thrown. If you want to serialize an object, basic rule is all Its dependencies must implements java.io.Serializable or you can make the non serialized variables as transient. Since class Employee has dependency on class Address, which is not implementing Serializable interface, when you tries to run above program, you will get following exception.

Exception in thread "main" java.io.NotSerializableException: Address
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1184)
 at java.io.ObjectOutputStream.defaultWriteFields(ObjectOutputStream.java:1548)
 at java.io.ObjectOutputStream.writeSerialData(ObjectOutputStream.java:1509)
 at java.io.ObjectOutputStream.writeOrdinaryObject(ObjectOutputStream.java:1432)
 at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1178)
 at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:348)
 at SerializeEmployee.main(SerializeEmployee.java:12)
Java Result: 1

To solve the above problem there are two ways.

Solution 1: Make the Address class implements Serializable interface.

import java.io.Serializable;

public class Address implements Serializable{
    String street,city,district,state;
    
    Address(String s, String c, String d, String st){
        street = s;
        city = c;
        district = d;
        state = st;
    }
}

Solution 2 : Declare the reference 'addr' of Address class as transient.

import java.io.Serializable;

public class Employee implements Serializable{
    int id;
    String name;
    transient Address addr;
    
    Employee(int id, String name, Address addr){
        this.id = id;
        this.name = name;
        this.addr = addr;
    }
}




                                                             Home

No comments:

Post a Comment