当oracle数据缺省值为数据类型编号为null时,处理空指针异常

时间:2017-10-30 17:46:04

标签: java oracle

我在Oracle数据库中有以下表格: enter image description here

我有实体类:

public class CollCmpCampaigns implements Serializable {
    private static final long serialVersionUID = 1L;

    private String campaignCode;
    private String campaignName;
    private Long showEventsDaysOut;
    private Long emailPreviewDaysPrior;
    private Long editAllowedDaysPrior;
    private Long warningDaysInCheckpoint;
    private Long eventDaysToPull;
    private Long uploadListDaysPrior;

   .... 
   //getters and setters

我在mapper类中有以下方法:

public com.***.catalogservices.generated.ws.CollCmpCampaigns mapCollCmpCampaignsToXML(CollCmpCampaigns ccc) {
        com.***.catalogservices.generated.ws.CollCmpCampaigns generatedCcc = new com.***.catalogservices.generated.ws.CollCmpCampaigns();

        generatedCcc.setCampaignCode(ccc.getCampaignCode());
        generatedCcc.setCampaignName(ccc.getCampaignName());
        generatedCcc.setShowEventsDaysOut(ccc.getShowEventsDaysOut());
        generatedCcc.setEmailPreviewDaysPrior(ccc.getEmailPreviewDaysPrior());
        generatedCcc.setEditAllowedDaysPrior(ccc.getEditAllowedDaysPrior());
        generatedCcc.setWarningDaysInCheckpoint(ccc.getWarningDaysInCheckpoint());
        generatedCcc.setEventDaysToPull(ccc.getEventDaysToPull());
        generatedCcc.setUploadListDaysPrior(ccc.getUploadListDaysPrior());
        return generatedCcc;
    }

调试时,我从查询中获得以下结果集:

CollCmpCampaigns[campaignCode=CMP1,
campaignName=Grad Event OnCampus_Full Campaign,
showEventsDaysOut=33,
emailPreviewDaysPrior=31,
editAllowedDaysPrior=24,
warningDaysInCheckpoint=33,
eventDaysToPull=0,
uploadListDaysPrior=<null>]

从调试结果中,您可以看到uploadListDaysPrior的值为<null>。 (显然)每当我尝试在getter值为null时设置值时,我都会遇到java.lang.NullPointerException异常。  
我可以通过设置这样的条件来解决这个问题:

if(ccc.getUploadListDaysPrior() != null){
    generatedCcc.setUploadListDaysPrior(ccc.getUploadListDaysPrior());
}

但这真的是一个好方法吗? 我是否要求DBA将data_default值0放在表中的Number类型而不是null中,或者我是否应该为所有条件设置上述条件?

1 个答案:

答案 0 :(得分:0)

从未意识到我的JAXB生成的类具有long类型而不是Long。将我的xsd文件(我在我的问题中从未提及)修改为:

<xs:complexType name="CollCmpCampaigns">
    <xs:sequence  minOccurs="0">
        <xs:element name="campaignCode" type="xs:string" />
        <xs:element name="campaignName" type="xs:string" />
        <xs:element name="showEventsDaysOut" type="xs:long" />
        <xs:element name="emailPreviewDaysPrior" type="xs:long" />
        <xs:element name="editAllowedDaysPrior" type="xs:long" />
        <xs:element name="warningDaysInCheckpoint" type="xs:long" />
        <xs:element name="eventDaysToPull" type="xs:long" />
        <xs:element name="uploadListDaysPrior" type="xs:long" />
    </xs:sequence>
</xs:complexType>

<xs:element name="GetCollCmpCampaignsResponse">
    <xs:complexType>
        <xs:complexContent>
            <xs:extension base="Response">
                <xs:sequence>
                    <xs:element name="CollCmpCampaigns" type="CollCmpCampaigns" minOccurs="0" maxOccurs="unbounded"/>
                </xs:sequence>
            </xs:extension>
        </xs:complexContent>
    </xs:complexType>
</xs:element>

然后我重新开始上课了,它有效!