3

我从在同一个项目中为我工作的相同 xml 开发了以下 xml。但是当我试图解组这些时,我得到了 NullPointerException。

我将我所有的 java 类与我正在解组的 xml 一起放置。

请帮忙

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<dbSchema>
    <table tableName="table1">
        <column columnName="column1">
            <dataType>varchar</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
            <isPrimaryKey>true</isPrimaryKey>
        </column>

        <column columnName="column2">
            <dataType>timestamp</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column3">
            <dataType>varchar</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column4">
            <dataType>decimal</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column5">
            <dataType>integer</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column6">
            <dataType>varchar2</dataType>
            <isNullAllowed>false</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column7">
            <dataType>date</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column8">
            <dataType>decimal</dataType>
            <isNullAllowed>false</isNullAllowed>
            <defaultValue></defaultValue>
        </column>
    </table>
    <table tableName="table2">
        <column columnName="column1">
            <dataType>varchar</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
            <isPrimaryKey>true</isPrimaryKey>
        </column>

        <column columnName="column2">
            <dataType>timestamp</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column3">
            <dataType>varchar</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column4">
            <dataType>decimal</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column5">
            <dataType>integer</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column6">
            <dataType>varchar2</dataType>
            <isNullAllowed>false</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column7">
            <dataType>date</dataType>
            <isNullAllowed>true</isNullAllowed>
            <defaultValue></defaultValue>
        </column>

        <column columnName="column8">
            <dataType>decimal</dataType>
            <isNullAllowed>false</isNullAllowed>
            <defaultValue></defaultValue>
        </column>
    </table>
</dbSchema>

@XmlRootElement(name="dbSchema")
public class DBSchema {

    private Vector<Table> tableList;

    /**
     * @return the tableList
     */
    public Vector<Table> getTableList() {
        return tableList;
    }

    /**
     * @param tableList the tableList to set
     */
    //XmLElementWrapper generates a wrapper element around XML representation
    //@XmlElementWrapper(name = "tableList") 
    //XmlElement sets the name of the entities
    @XmlElement(name = "table")
    public void setTabelList(Vector<Table> tableList) {
        this.tableList = tableList;
    }
}

@XmlRootElement(name = "table")
public class Table {

    private String tableName;

    private Vector<Column> columnList;

    public Table() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @return the tableName
     */
    public String getTableName() {
        return tableName;
    }

    /**
     * @param pageId the pageId to set
     */
    @XmlAttribute(name="tableName")
    public void setTableName(String tableName) {
        this.tableName = tableName;
    }

    /**
     * @return the columnList
     */
    public Vector<Column> getColumnList() {
        return columnList;
    }

    /**
     * @param columnList the columnList to set
     */
    //XmLElementWrapper generates a wrapper element around XML representation
    //@XmlElementWrapper(name = "columnList") 
    //XmlElement sets the name of the entities
    @XmlElement(name = "column")
    public void setColumnList(Vector<Column> columnList) {
        this.columnList = columnList;
    }

}

@XmlRootElement(name = "column")
public class Column {

    private String columnName = "";

    private String dataType = "";

    private boolean isNullAllowed = false;

    private boolean isPrimaryKey = false;

    private String defaultValue = "";

    public Column() {
        // TODO Auto-generated constructor stub
    }

    /**
     * @return the columnName
     */
    public String getColumnName() {
        return columnName;
    }

    /**
     * @param columnName the columnName to set
     */
    @XmlAttribute
    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    /**
     * @return the dataType
     */
    public String getDataType() {
        return dataType;
    }

    /**
     * @param dataType the dataType to set
     */
    @XmlElement
    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    /**
     * @return the isNullAllowed
     */
    public boolean isNullAllowed() {
        return isNullAllowed;
    }

    /**
     * @param isNullAllowed the isNullAllowed to set
     */
    @XmlElement
    public void setIsNullAllowed(boolean isNullAllowed) {
        this.isNullAllowed = isNullAllowed;
    }

    /**
     * @return the isPrimaryKey
     */
    public boolean isPrimaryKey() {
        return isPrimaryKey;
    }

    /**
     * @param isPrimaryKey the isPrimaryKey to set
     */
    @XmlElement
    public void setIsPrimaryKey(boolean isPrimaryKey) {
        this.isPrimaryKey = isPrimaryKey;
    }

    /**
     * @return the defaultValue
     */
    public String getDefaultValue() {
        return defaultValue;
    }

    /**
     * @param defaultValue the defaultValue to set
     */
    @XmlElement
    public void setDefaultValue(String defaultValue) {
        this.defaultValue = defaultValue;
    }

}

Exception in thread "main" java.lang.NullPointerException
    at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:290)
    at com.sun.xml.internal.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:254)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:106)
    at com.sun.xml.internal.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:195)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:524)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:143)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2939)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:648)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:140)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:511)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:808)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:737)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:119)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1205)
    at com.sun.org.apache.xerces.internal.jaxp.SAXParserImpl$JAXPSAXParser.parse(SAXParserImpl.java:522)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:200)
    at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:173)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:137)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:142)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:151)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:169)
    at com.misys.importexportdata.helpers.SQLHandler.readDBDefinition(SQLHandler.java:99)
    at com.misys.importexportdata.helpers.SQLHandler.main(SQLHandler.java:29)


    private void readDBDefinition() {
        List<String> schemaList = FileHandler.getDirectoryContent(DefinedConstants.DB_DEFINITION_LOCATION);
        for (int i = 0; i < schemaList.size(); i++ ) {
            DBSchema schemaDetails = null;
            String schemaName = schemaList.get(i) ;
            if(schemaName.endsWith(".xml")) {
                try {
                    JAXBContext context = JAXBContext.newInstance(DBSchema.class);
                    Unmarshaller unMarshaller = context.createUnmarshaller();
                    File dbDefinitionFile = new File(DefinedConstants.DB_DEFINITION_LOCATION + schemaName);
                    schemaDetails = (DBSchema) unMarshaller.unmarshal(dbDefinitionFile);
                } catch (JAXBException e) {
                    e.printStackTrace();
                }
            }
            tableMap.put(schemaName, schemaDetails.getTableList()); 
        }
    }

我得到的 Stacktrace 是这样结束的:

java.lang.NullPointerException
    at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:305)
    at com.sun.xml.bind.v2.runtime.reflect.Lister$CollectionLister.addToPack(Lister.java:269)
    at com.sun.xml.bind.v2.runtime.unmarshaller.Scope.add(Scope.java:121)
    at com.sun.xml.bind.v2.runtime.property.ArrayERProperty$ReceiverImpl.receive(ArrayERProperty.java:213)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.endElement(UnmarshallingContext.java:538)
    at com.sun.xml.bind.v2.runtime.unmarshaller.ValidatingUnmarshaller.endElement(ValidatingUnmarshaller.java:107)
    at com.sun.xml.bind.v2.runtime.unmarshaller.SAXConnector.endElement(SAXConnector.java:158)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:609)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanEndElement(XMLDocumentFragmentScannerImpl.java:1782)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl$FragmentContentDriver.next(XMLDocumentFragmentScannerImpl.java:2973)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentScannerImpl.next(XMLDocumentScannerImpl.java:606)
    at com.sun.org.apache.xerces.internal.impl.XMLNSDocumentScannerImpl.next(XMLNSDocumentScannerImpl.java:117)
    at com.sun.org.apache.xerces.internal.impl.XMLDocumentFragmentScannerImpl.scanDocument(XMLDocumentFragmentScannerImpl.java:510)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:848)
    at com.sun.org.apache.xerces.internal.parsers.XML11Configuration.parse(XML11Configuration.java:777)
    at com.sun.org.apache.xerces.internal.parsers.XMLParser.parse(XMLParser.java:141)
    at com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.parse(AbstractSAXParser.java:1213)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:258)
    at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:229)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:140)
    at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:123)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:754)
    at org.springframework.oxm.jaxb.Jaxb2Marshaller.unmarshal(Jaxb2Marshaller.java:735)

更新 1: SQLHandler 是具有 main 方法的 java 类。

更新2:伙计们,我发现了问题。这个问题非常愚蠢。我把注释拼错了。这导致了问题。

4

2 回答 2

1

通常 JAXB 将调用get一个Collection属性,然后调用add它。尝试先初始化您的集合(tableListcolumnList)。

于 2012-10-02T14:11:11.523 回答
0

问题的答案在于官方 JAXB 论坛的这句话:

JAXB unmarshaller 不期望 List 类型的属性的 setter;它只会使用 x.getSomeList().add( y ) 添加另一个孩子。请参阅此示例以了解编写列表 getter 的典型方式(这样做有几个很好的理由)。

重要的不是你是否初始化你的集合,而是你的集合的getter 方法不返回 null

public class ListsType {

    protected List<Integer> numbers;

    public List<Integer> getNumbers() {
        if (numbers == null) {
            numbers = new ArrayList<Integer>();
        }
        return this.numbers;
    }
}

有关完整示例,请单击此处

于 2015-07-29T10:03:58.117 回答