2012-10-02 91 views
3

期間我已經從在同一項目上工作,我同一個XML的開發下面的XML的。但是當我試圖解開這些時,我得到了NullPointerException。NullPointerException異常XML解組

我把我所有的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()); 
     } 
    } 

堆棧跟蹤我正在結束這樣的:

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是java類的主要方法。

更新2:傢伙我已經找到了問題。問題很愚蠢。我錯誤拼寫了註釋。這導致了問題。

+0

是什麼線SQLHandler.java:29? –

+0

@StefanBe - SQLHandler是帶有main方法的java類。 – dareurdream

回答

1

通常JAXB將撥打get撥打Collection的房產,然後撥打add。嘗試初始化您的收藏(tableListcolumnList)。

+0

我不確定誰在這裏。請看看鏈接 - http://java.net/projects/jaxb/lists/users/archive/2011-05/message/52 – dareurdream

+0

試試看吧! :) –

+0

沒有影響。一樣的。無論如何,我已找到解決方案,並在上面更新。感謝您的時間和答案。 – dareurdream

0

的答案,問題就出在這句話從official JAXB forum

的JAXB解組不期望類型 名單的性質制定者;它只會使用x.getSomeList()。add(y)添加另一個子項。 爲列表寫 (有幾個很好的理由做這種方式)的典型方式干將看到這個例子。

重要的是沒有這麼多,你是否初始化集合,而是爲你收集的getter方法沒有返回null

public class ListsType { 

    protected List<Integer> numbers; 

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

有關完整的示例,請點擊here