我有一个用于克隆的XStream。这是我的简单代码,我对它的专业知识不多。
com.thoughtworks.xstream.XStream XSTREAM = new com.thoughtworks.xstream.XStream();
稍后我将克隆实例存储在hashTable中(我知道存储它并不是一个好主意,但它是一个遗留系统)。
我存储了一类学生,后来我存储(克隆)学生的其他实例并加注。
com.thoughtworks.xstream.converters.reflection.ObjectAccessException: Could not call com.model.Student_$$_javassist_83.writeReplace():null java.lang.NullPointerException
这是我存储克隆对象的代码。
public void keep(String key, Object value)
{
Object obj = XSTREAM.fromXML(XSTREAM.toXML(value));
storage.put(key,obj);
}
我认为这是问题产生的源代码。 [XStream来源。] [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;
}
}
我希望有人可以指导我,我在这个话题上有点迷失。
这是追踪。当我清除clazz示例student.setListOfPhones(null)
的依赖性时,它似乎有效;例外是图表中的2或3级别。为什么根据XStream它说:
使其适用于大型对象图或具有高消息吞吐量的系统。
堆栈追踪:
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)
答案 0 :(得分:1)
嗨,我已经解决了问题,另一种方法是在这个类Student上创建一个浅层副本,当然当XStream得到类时,关系不够深入XStream并且NullPointerException被抛出。感谢Dave god Bless。