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!
Your Object should implement Serializable
interface
HINT: For clone the object, far better to implement Cloneable
interface and use the object.clone()
method
这意味着java.lang.Object
是不可序列化的,它没有实现Serializable
,可能是您将Object
类的Object
传递给了您的方法。
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
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.
The Object
class does not implement the Serializable
interface, so it cannot be used directly with object streams. More details here .
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.
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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.