0
package example;

import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.lang.Object;

class Utils {
    public static Object copy(Object oldObj) {
        Object obj = null;
        try {
            // Write the object out to a byte array
            ByteArrayOutputStream bos = new ByteArrayOutputStream();
            ObjectOutputStream out = new ObjectOutputStream(bos);
            out.writeObject(oldObj);
            out.flush();
            out.close();

            // Retrieve an input stream from the byte array and read
            // a copy of the object back in.
            ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
            ObjectInputStream in = new ObjectInputStream(bis);
            obj = in.readObject();
        } catch (IOException e) {
            e.printStackTrace();   
        } catch (ClassNotFoundException cnfe) {
            cnfe.printStackTrace();
        }
        return obj;
    }
}

public class mytest {
    public static void main(String[] args) throws IOException {
        Object clonedObject = Utils.copy(new Object());
        clonedObject.notifyAll();
    }
}

Above code is to show how deep copy works by changing a object to byte array. But myeclipse gives below error messages and I don't know why.

java.io.NotSerializableException: java.lang.Object
    at java.io.ObjectOutputStream.writeObject0(ObjectOutputStream.java:1156)
    at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:326)
    at example.Utils.copy(mytest.java:17)
    at example.mytest.main(mytest.java:37)
Exception in thread "main" java.lang.NullPointerException
    at example.mytest.main(mytest.java:38)

Could you please help? Thanks!

Tom Xue
  • 3,169
  • 7
  • 40
  • 77

7 Answers7

1

Your Object should implement Serializable interface

HINT: For clone the object, far better to implement Cloneable interface and use the object.clone() method

SeniorJD
  • 6,946
  • 4
  • 36
  • 53
1

It means that java.lang.Object is not serializable, it doesn't implement Serializable, probably you are passing an object of class Object to your method.

morgano
  • 17,210
  • 10
  • 45
  • 56
1

you are trying to serialize not Serializable (that is, it doesn't implement the interface Serializable) object

try this :

Object clonedObject = Utils.copy(new String("Hello");

String class is Serializable

Mohammad Ersan
  • 12,304
  • 8
  • 54
  • 77
1

When you do writeObject, the objevt you write needs to be Serializable. Try to change the signature of your copy-method to

public static Object copy(Serializable oldObj)

The error message will be clearer.

Bex
  • 2,905
  • 2
  • 33
  • 36
0

The Object class does not implement the Serializable interface, so it cannot be used directly with object streams. More details here.

Nicola Musatti
  • 17,834
  • 2
  • 46
  • 55
0

From the documentation of ObjectOutputStream.writeObject:

Throws:

NotSerializableException - Some object to be serialized does not implement the java.io.Serializable interface.

This makes sense, because you call your method with the parameter new Object(). Indeed, Object does not implement Serializable. I wonder, however, why the signature of writeObject isn't simply

writeObject(Serializable object)

which would prevent this kind of errors.

Community
  • 1
  • 1
Vincent van der Weele
  • 12,927
  • 1
  • 33
  • 61
  • This is one of java's retrofitting dinosaurs. Another one is the Cloneable-interface and its implementation in Object! – Bex Aug 13 '13 at 09:42
0

It is saying that your object is not eligible for Serialization process because it is not implemented Serilizable interface.
According to java docs

java.io.NotSerializableException Thrown when an instance is required to have a Serializable interface. The serialization runtime or the class of the instance can throw this exception.

Prabhaker A
  • 8,317
  • 1
  • 18
  • 24