问题
I have a XStream for cloning. Here is my simple code, I have not much expertise with it.
com.thoughtworks.xstream.XStream XSTREAM = new com.thoughtworks.xstream.XStream();
Later I store the clone instances in a hashTable (I know is not very good idea storing on it but is a legacy system).
I store it a class of Student, later I store (clone) other instance of Student and raises.
com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Could not call com.model.Student_$$_javassist_83.writeReplace():null java.lang.NullPointerException
Here is my code for storing the clone objects.
public void keep(String key, Object value)
{
Object obj = XSTREAM.fromXML(XSTREAM.toXML(value));
storage.put(key,obj);
}
I think here is the source code where the problem is arising. [XStream sources.][Snippet]
public Object callWriteReplace(Object object)
{
Method writeReplaceMethod = getMethod(object.getClass(), "writeReplace", null, true);
if (writeReplaceMethod != null) {
try {
Object[] EMPTY_ARGS = new Object[0];
return writeReplaceMethod.invoke(object, EMPTY_ARGS);
} catch (IllegalAccessException e) {
throw new ObjectAccessException("Could not call " + object.getClass().getName() + ".writeReplace()", e);
} catch (InvocationTargetException e) {
throw new ObjectAccessException("Could not call " + object.getClass().getName() + ".writeReplace()", e.getTargetException());
}
} else {
return object;
}
}
I hope somebody can guide me, I am a little lost on this topic.
Here is the trace. It seems when I clean the dependences of the clazz example student.setListOfPhones(null)
it works; the exception is throw on the 2 or 3 level in the graph. Why is this according to XStream it says:
making it suitable for large object graphs or systems with high message throughput.
Stack trace:
com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Could not call com.model.Subjects_$$_javassist_224.writeReplace() : null
java.lang.NullPointerException
at javassist.util.proxy.RuntimeSupport$DefaultMethodHandler.invoke(RuntimeSupport.java:37)
at com.model.Subjects_$$_javassist_224.writeReplace(Subject_$$_javassist_224.java)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.thoughtworks.xstream.converters.reflection.SerializationMethodInvoker.callWriteReplace(SerializationMethodInvoker.java:88)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:60)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshallField(AbstractReflectionConverter.java:229)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.writeField(AbstractReflectionConverter.java:208)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$2.<init>(AbstractReflectionConverter.java:171)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.doMarshal(AbstractReflectionConverter.java:116)
at com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.marshal(AbstractReflectionConverter.java:72)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:69)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:58)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller$1.convertAnother(AbstractReferenceMarshaller.java:84)
回答1:
hi i have solved the problem was another method was creating a shallow copy on this class Student and of course when XStream got the class the relationship wasn't deep enough for XStream and NullPointerException was Throw.. thank Dave god Bless.
来源:https://stackoverflow.com/questions/16466032/java-xstream-deep-copy-raises-exception-objectaccessexception