Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

[Solved] java.io.NotSerializableException in Java

In this post, I will be sharing how to fix java.io.NotSerializableException in Java. According to Oracle docs, this exception is thrown when an instance is required to have a Serializable interface. This exception is thrown by either the instance of the Class or by the serialization runtime. It is a checked exception since it extends ObjectStreamException class which in turn extends IOException class.

Read Also: [Fixed] java.io.EOFException in Java

java.io.NotSerializableException class was introduced in the 1.1 version of the Java Development Kit(JDK). Let's dive deep into the topic:

[Fixed] java.io.NotSerializableException in Java

As always, first, we will produce the java.io.NotSerializableException before moving on to the solution.

1. Producing the exception


java.io.NotSerializableException can be thrown when we try to serialize an object of the class that does not implement the Serializable interface as shown below in the example.

import java.io.IOException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;

// Employee is a Helper class
class Employee {
private String id;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
// Driver class or Main class
public class NotSerializableExceptionExample
{
public static void main(String args[]) throws IOException
{
// Creating FileOutputStream object to create an employee.txt file
FileOutputStream fos = new FileOutputStream("employee.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Employee empObj = new Employee();
empObj.setId("08");
// Writing empObj object to stream
oos.writeObject(empObj);
// Closing the stream
oos.close();
        System.out.println("Alive is awesome");
    }
}


Output:
Exception in thread "main" java.io.NotSerializableException: Employee
      at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1197)

Solution

1. Implementing the Serializable interface


The simplest solution to the NotSerializableException is to find the class that throws the exception and makes it implement the Serializable interface. In the above code, you can see that the helper class Employee does not implement the Serializable interface. Hence, we got the java.io.NotSerializableException.

By implementing a Serializable interface, we are allowing an Employee object to be stored in the employee.txt file as shown below in the code:

import java.io.IOException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
// Employee is a Helper class 
class Employee implements Serializable {
private String id;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
// Driver class or Main class
public class NotSerializableExceptionExample
{
public static void main(String args[]) throws IOException
{
// Creating FileOutputStream object to create an employee.txt file
FileOutputStream fos = new FileOutputStream("employee.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Employee empObj = new Employee();
empObj.setId("08");
// Writing empObj object to stream
oos.writeObject(empObj);
// Closing the stream
oos.close();
System.out.println("Alive is awesome");
}
}


Output:
Alive is awesome


The above solution is not feasible if the class that throws the java.io.NotSerializableException belongs to a third-party library. If you do not want a particular field to be serialized then you can mark it as transient. Once a field of the class is marked as transient, then it will be ignored by the serializable runtime.

2. Producing the exception using Inner class


We can also get java.io.NotSerializableException while using the inner class as shown below in the example:

import java.io.IOException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


// Outer class
public class NotSerializableExceptionExample2
{
// Inner class
class Employee implements Serializable {
private String id;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
// Main method
public static void main(String args[]) throws IOException
{
NotSerializableExceptionExample2 obj = new NotSerializableExceptionExample2();
obj.serializeEmployee();
System.out.println("Alive is awesome");
}

private void serializeEmployee() throws IOException
{
// Creating FileOutputStream object to create an employee.txt file
FileOutputStream fos = new FileOutputStream("employee.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Employee empObj = new Employee();
empObj.setId("10");
// Writing empObj object to stream
oos.writeObject(empObj);
// Closing the stream
oos.close();
}
}


Output:
Exception in thread "main" java.io.NotSerializableException: NotSerializableExceptionExample2
      at java.base/java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1197)

Solution

2. Implementing the Serializable interface in the Outer class

To get rid of this exception we have to make the outer class serializable. This can be done by implementing the Serializable interface as shown below in the example:

import java.io.IOException;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;


// Outer class
public class NotSerializableExceptionExample2 implements Serializable
{
// Inner class
class Employee implements Serializable {
private String id;
public String getId()
{
return id;
}
public void setId(String id)
{
this.id = id;
}
}
// Main method
public static void main(String args[]) throws IOException
{
NotSerializableExceptionExample2 obj = new NotSerializableExceptionExample2();
obj.serializeEmployee();
System.out.println("Alive is awesome");
}

private void serializeEmployee() throws IOException
{
// Creating FileOutputStream object to create an employee.txt file
FileOutputStream fos = new FileOutputStream("employee.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
Employee empObj = new Employee();
empObj.setId("10");
// Writing empObj object to stream
oos.writeObject(empObj);
// Closing the stream
oos.close();
}
}


Output:
Alive is awesome


According to Oracle docs, the Serialization of inner classes is strongly discouraged for several reasons.

That's all for today. Please mention in the comments if you have any questions related to how to solve java.io.NotSerializableException in Java.


This post first appeared on Java Hungry, please read the originial post: here

Share the post

[Solved] java.io.NotSerializableException in Java

×

Subscribe to Java Hungry

Get updates delivered right to your inbox!

Thank you for your subscription

×