Tom Xue
Tom Xue

Reputation: 3355

What does the error info means?

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!

Upvotes: 0

Views: 986

Answers (7)

Mohammad Ersan
Mohammad Ersan

Reputation: 12444

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

Upvotes: 1

Prabhaker A
Prabhaker A

Reputation: 8473

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.

Upvotes: 0

Vincent van der Weele
Vincent van der Weele

Reputation: 13177

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.

Upvotes: 0

Bex
Bex

Reputation: 2925

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.

Upvotes: 1

Nicola Musatti
Nicola Musatti

Reputation: 18218

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

Upvotes: 0

morgano
morgano

Reputation: 17422

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.

Upvotes: 1

SeniorJD
SeniorJD

Reputation: 7202

Your Object should implement Serializable interface

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

Upvotes: 1

Related Questions