Reputation: 51
I have a dictionary-style application I'm working on with Java, and I'm using the Simple framework for serialization. It includes an ArrayList of Strings, and the only problem I've encountered is that Simple will serialize/deserialize those Strings and change any empty values to null. I understand from this question that you need to use a Converter. I followed what was described on that page and added the class that Sand wrote in his answer (with the "new AnnotationStrategy()" change needed), and now this is what the main class that I'm having problems with looks like (with the numerous get/set methods stripped):
public class Item {
@ElementList(name = "itemContents")
@Convert(SimpleXMLStringConverter.class)
private ArrayList<String> values;
public Item()
{
}
}
Whenever I try to write the class to an XML file, I get this error:
org.simpleframework.xml.convert.ConvertException: Element annotation required for field 'values' private java.util.ArrayList main.LangComp.Item.values
at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:147)
at org.simpleframework.xml.convert.ConverterScanner.getConvert(ConverterScanner.java:121)
at org.simpleframework.xml.convert.ConverterScanner.getConverter(ConverterScanner.java:100)
at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:175)
at org.simpleframework.xml.convert.AnnotationStrategy.write(AnnotationStrategy.java:155)
at org.simpleframework.xml.core.Source.setOverride(Source.java:394)
at org.simpleframework.xml.core.Factory.setOverride(Factory.java:170)
at org.simpleframework.xml.core.Composite.isOverridden(Composite.java:1387)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1309)
at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
at org.simpleframework.xml.core.CompositeList.write(CompositeList.java:248)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1331)
at org.simpleframework.xml.core.Composite.writeElement(Composite.java:1314)
at org.simpleframework.xml.core.Composite.writeUnion(Composite.java:1194)
at org.simpleframework.xml.core.Composite.writeElements(Composite.java:1165)
at org.simpleframework.xml.core.Composite.writeSection(Composite.java:1071)
at org.simpleframework.xml.core.Composite.write(Composite.java:1042)
at org.simpleframework.xml.core.Composite.write(Composite.java:1019)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:236)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:208)
at org.simpleframework.xml.core.Traverser.write(Traverser.java:186)
at org.simpleframework.xml.core.Persister.write(Persister.java:1187)
at org.simpleframework.xml.core.Persister.write(Persister.java:1169)
at org.simpleframework.xml.core.Persister.write(Persister.java:1147)
at org.simpleframework.xml.core.Persister.write(Persister.java:1266)
at org.simpleframework.xml.core.Persister.write(Persister.java:1248)
at org.simpleframework.xml.core.Persister.write(Persister.java:1229)
at org.simpleframework.xml.core.Persister.write(Persister.java:1208)
at main.IO.FileHandler.WriteXML(FileHandler.java:32)
at main.UI.MainUI.formWindowClosing(MainUI.java:108)
at main.UI.MainUI.access$000(MainUI.java:16)
at main.UI.MainUI$1.windowClosing(MainUI.java:76)
at java.awt.Window.processWindowEvent(Window.java:2045)
at javax.swing.JFrame.processWindowEvent(JFrame.java:296)
at java.awt.Window.processEvent(Window.java:2003)
at java.awt.Component.dispatchEventImpl(Component.java:4861)
at java.awt.Container.dispatchEventImpl(Container.java:2287)
at java.awt.Window.dispatchEventImpl(Window.java:2713)
at java.awt.Component.dispatchEvent(Component.java:4687)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:707)
at java.awt.EventQueue.access$000(EventQueue.java:101)
at java.awt.EventQueue$3.run(EventQueue.java:666)
at java.awt.EventQueue$3.run(EventQueue.java:664)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:87)
at java.awt.EventQueue$4.run(EventQueue.java:680)
at java.awt.EventQueue$4.run(EventQueue.java:678)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:677)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:211)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:128)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:117)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:113)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:90)
I was able to get this program to work before, with the only problem being that it would turn empty strings to null, which is something I need to avoid. I can't figure out what's causing the problem or how to fix it. Is it because the converter class I got from the question I linked is for strings, not ArrayLists, or does it not matter because the XML nodes are strings and not lists anyway? I'm having trouble understanding what annotation it wants when it says "Element annotation required for field".
Upvotes: 1
Views: 558
Reputation: 51
The error message was being thrown because I was using @ElementList, and this process requires @Element. I found a way to make it work with ArrayLists because I gave up on trying to make it work in one instance and just wanted the converter to apply to EVERYTHING. I don't know if it's a good approach but it works. I just followed the steps described here to create a RegistryStrategy, and instead of binding the converter to my class (which it wouldn't accept), I bound it to String.class; in other words, the serialization process doesn't care about if it's stored in a complex class, ArrayList, ect, if it is a String then it is going to be set to "" if it's read in as null.
Upvotes: 2