Stavo effettuando il parsing di un xml utilizzando JAXB.
L'xml aveva una struttura di questo tipo
<root>
<lista>
<classpathentry kind="var" path="M2_REPO/javax/transaction/jta/1.0.1B/jta-1.0.1B.jar"/>
.......
</lista>
</root>
La classe JAXB era definita così:
package it.eng.classpathreader;
import java.util.Collection;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="root")
public class MyXml {
private Collection<ClassPathElement> lista;
@XmlElementWrapper(name="lista",nillable=true,required=false)
@XmlElement(name="classpathentry",nillable=true,required=false)
public Collection<ClassPathElement> getListaElementi() {
return lista;
}
public void setListaUtenti(Collection<ClassPathElement> listaEl) {
this.lista = listaEl;
}
public String toString(){
StringBuilder sb=new StringBuilder();
for(ClassPathElement i : lista){
sb.append(i);
sb.append("\n");
}
return sb.toString();
}
}
Andando a parsare l'Xm l ho avuto questo errore:
java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at it.eng.classpathreader.Reader.main(Reader.java:21)
Dopo qualche "googlata" ho trovato il problema: la Collection rappresentante la lista di nodi di tipo classpathentry va inizializzata.
Quindi invece di
private Collection<ClassPathElement> lista;
ho scritto
private Collection<ClassPathElement> lista=new ArrayList<ClassPathElement>();
e tutto ha funzionato a dovere.
L'xml aveva una struttura di questo tipo
<root>
<lista>
<classpathentry kind="var" path="M2_REPO/javax/transaction/jta/1.0.1B/jta-1.0.1B.jar"/>
.......
</lista>
</root>
La classe JAXB era definita così:
package it.eng.classpathreader;
import java.util.Collection;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="root")
public class MyXml {
private Collection<ClassPathElement> lista;
@XmlElementWrapper(name="lista",nillable=true,required=false)
@XmlElement(name="classpathentry",nillable=true,required=false)
public Collection<ClassPathElement> getListaElementi() {
return lista;
}
public void setListaUtenti(Collection<ClassPathElement> listaEl) {
this.lista = listaEl;
}
public String toString(){
StringBuilder sb=new StringBuilder();
for(ClassPathElement i : lista){
sb.append(i);
sb.append("\n");
}
return sb.toString();
}
}
Andando a parsare l'Xm l ho avuto questo errore:
java.lang.NullPointerException
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractXMLDocumentParser.emptyElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.scanStartElement(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(Unknown Source)
at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(Unknown Source)
at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(Unknown Source)
at it.eng.classpathreader.Reader.main(Reader.java:21)
Dopo qualche "googlata" ho trovato il problema: la Collection rappresentante la lista di nodi di tipo classpathentry va inizializzata.
Quindi invece di
private Collection<ClassPathElement> lista;
ho scritto
private Collection<ClassPathElement> lista=new ArrayList<ClassPathElement>();
e tutto ha funzionato a dovere.
Nessun commento:
Posta un commento