Unknown field exception

classic Classic list List threaded Threaded
2 messages Options
Reply | Threaded
Open this post in threaded view
|

Unknown field exception

Anjib Mulepati
Hi All,

I have following classes and XML file

public class CodeCatalog {
   CodeTable codeList;
   //getter and setter
}

public class CodeTable {
   private String name;
   private List<CodeInfo> codeInfoList = new ArrayList<CodeInfo>();  
   //getter and setter
}

public class CodeInfo {
   private String code;
   private String name;
   private String description;
   //getter and setter
}

<?xml version="1.0" encoding="UTF-8"?>
<codecatalog>
  <codetable name="sample1">
     <codeinfo>
       <code>123</code>
       <name>Test</name>
       <description>Test code name 1</description>
     </codeinfo>
     <codeinfo>
       <code>456</code>
       <name>Beta</name>
       <description>Test code name 2</description>
     </codeinfo>
  </codetable>
</codecatalog>

And I have following main class

public class ConvertApp {
   public static void main(String[] args) {
     File file = new File("C:\\sampleXML.xml");
     XStream xstream = new XStream();
     xstream.alias("codecatalog", CodeCatalog.class);
     xstream.alias("codetable", CodeTable.class);
     xstream.alias("codeinfo", CodeInfo.class);
     xstream.useAttributeFor(CodeTable.class, "name");
     xstream.addImplicitCollection(CodeTable.class, "codeInfoList");
     CodeCatalog codeCatalog = (CodeCatalog)xstream.fromXML(file);
     System.out.println(codeCatalog.getCodeList().getCodeInfoList().get(1).
getCode());
     System.out.println(codeCatalog.getCodeList().getName());
  }
}

I am getting following error

Exception in thread "main"
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$U
nknownFieldException: No such field com.anjib.lab.CodeCatalog.codelist
---- Debugging information ----
field               : codelist
class               : com.anjib.lab.CodeCatalog
required-type       : com.anjib.lab.CodeCatalog
converter-type      :
com.thoughtworks.xstream.converters.reflection.ReflectionConverter
path                : /codecatalog/codelist
line number         : 3
version             : 1.4.5
-------------------------------
        at
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.h
andleUnknownField(AbstractReflectionConverter.java:485)
        at
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.d
oUnmarshal(AbstractReflectionConverter.java:341)
        at
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.u
nmarshal(AbstractReflectionConverter.java:247)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java
:72)
        at
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstract
ReferenceUnmarshaller.java:65)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshall
er.java:66)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshall
er.java:50)
        at
com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:1
34)
        at
com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(Abst
ractTreeMarshallingStrategy.java:32)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1156)
        at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1140)
        at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1104)
        at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1046)
        at com.anjib.lab.ConvertApp.main(ConvertApp.java:17)

What I am doing wrong?




---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply | Threaded
Open this post in threaded view
|

Re: Unknown field exception

Jörg Schaible-4
Hi,

Anjib Mulepati wrote:

> Hi All,
>
> I have following classes and XML file
>
> public class CodeCatalog {
>    CodeTable codeList;
>    //getter and setter
> }
>
> public class CodeTable {
>    private String name;
>    private List<CodeInfo> codeInfoList = new ArrayList<CodeInfo>();
>    //getter and setter
> }
>
> public class CodeInfo {
>    private String code;
>    private String name;
>    private String description;
>    //getter and setter
> }
>
> <?xml version="1.0" encoding="UTF-8"?>
> <codecatalog>
>   <codetable name="sample1">
>      <codeinfo>
>        <code>123</code>
>        <name>Test</name>
>        <description>Test code name 1</description>
>      </codeinfo>
>      <codeinfo>
>        <code>456</code>
>        <name>Beta</name>
>        <description>Test code name 2</description>
>      </codeinfo>
>   </codetable>
> </codecatalog>
>
> And I have following main class
>
> public class ConvertApp {
>    public static void main(String[] args) {
>      File file = new File("C:\\sampleXML.xml");
>      XStream xstream = new XStream();
>      xstream.alias("codecatalog", CodeCatalog.class);
>      xstream.alias("codetable", CodeTable.class);
>      xstream.alias("codeinfo", CodeInfo.class);
>      xstream.useAttributeFor(CodeTable.class, "name");
>      xstream.addImplicitCollection(CodeTable.class, "codeInfoList");
>      CodeCatalog codeCatalog = (CodeCatalog)xstream.fromXML(file);
>      
System.out.println(codeCatalog.getCodeList().getCodeInfoList().get(1).
> getCode());
>      System.out.println(codeCatalog.getCodeList().getName());
>   }
> }
>
> I am getting following error
>
> Exception in thread "main"
>
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter$U

> nknownFieldException: No such field com.anjib.lab.CodeCatalog.codelist
> ---- Debugging information ----
> field               : codelist
> class               : com.anjib.lab.CodeCatalog
> required-type       : com.anjib.lab.CodeCatalog
> converter-type      :
> com.thoughtworks.xstream.converters.reflection.ReflectionConverter
> path                : /codecatalog/codelist
> line number         : 3
> version             : 1.4.5
> -------------------------------
> at
>
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.h
> andleUnknownField(AbstractReflectionConverter.java:485)
> at
>
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.d
> oUnmarshal(AbstractReflectionConverter.java:341)
> at
>
com.thoughtworks.xstream.converters.reflection.AbstractReflectionConverter.u
> nmarshal(AbstractReflectionConverter.java:247)
> at
>
com.thoughtworks.xstream.core.TreeUnmarshaller.convert(TreeUnmarshaller.java
> :72)
> at
>
com.thoughtworks.xstream.core.AbstractReferenceUnmarshaller.convert(Abstract
> ReferenceUnmarshaller.java:65)
> at
>
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshall
> er.java:66)
> at
>
com.thoughtworks.xstream.core.TreeUnmarshaller.convertAnother(TreeUnmarshall
> er.java:50)
> at
>
com.thoughtworks.xstream.core.TreeUnmarshaller.start(TreeUnmarshaller.java:1
> 34)
> at
>
com.thoughtworks.xstream.core.AbstractTreeMarshallingStrategy.unmarshal(Abst
> ractTreeMarshallingStrategy.java:32)
> at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1156)
> at com.thoughtworks.xstream.XStream.unmarshal(XStream.java:1140)
> at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1104)
> at com.thoughtworks.xstream.XStream.fromXML(XStream.java:1046)
> at com.anjib.lab.ConvertApp.main(ConvertApp.java:17)
>
> What I am doing wrong?

Your collection is not implicit in the XML. Please read the following
advice: http://xstream.codehaus.org/faq.html#XML_unmarshalling_fails

Cheers,
Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email