use*_*864 2 java exception xmlencoder instantiationexception
我试图在我的 Java 程序中使用 XMLEncoder,但我收到了 java.lang.InstantiationException。以下是我正在使用的代码:
/*
Method for serialization.
*/
public void serializeToXml(Object obj) throws FileNotFoundException{
FileOutputStream fos = new FileOutputStream("/home/neeraj/xmlOP.xml");
XMLEncoder encoder = new XMLEncoder(fos);
encoder.writeObject(obj);
encoder.close();
}
public static void main(String [] args){
String uuid = UUID.randomUUID().toString();
SimpleDateFormat format = new SimpleDateFormat("dd/mm/yyyy");
Date date = new Date();
String tDate = format.format(date);
ClassA a = new ClassA(uuid,"expense","Pune",tDate,1,200,0,4);
a.createAssociatedEvents(2);
serializationExample serializer = new serializationExample();
try {
serializer.serializeToXml(a);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
除此之外:我还有两个类:classA 和 classB。这两个类都实现了 Serializable。ClassA 有一个 ClassB 的 ArrayList。两个类的所有字段都有 getter 和 setter 方法。确切的错误(堆栈跟踪)是;
java.lang.InstantiationException: classA continuing...
java.lang.exception :XMLEncoder:discarding statement XMLEncoder.writeObject(classA);
continuing.
Run Code Online (Sandbox Code Playgroud)
我无法弄清楚出了什么问题或这些错误意味着什么。我应该如何纠正我的代码以使其正常工作?
谢谢。