Java DOM Parser在某些字符串上返回空指针,但在其他字符串上不返回

时间:2011-09-06 12:46:06

标签: java xml dom jodatime

我不知道是什么原因造成的。在我下面附带的函数中,我有三个字符串正确地从XML文件中读取,三个字符串标记为“问题”,所有这些都会导致空指针异常。它们的完成方式与前三个完全相同。我尝试过的一些事情包括将呼叫改为:

act.RemainingLateFinishDate  = new DateTime(initElement.getElementsByTagName("RemainingLateFinishDate").item(0).getNodeValue());

这不再导致空指针异常,而是似乎返回一个空字符串,这同样没用。我尝试的另一件事是从JODA DateTime构造函数中获取所有DOM的东西,正如您可能知道的那样,它没有丝毫差别。

这是功能:

private Activity getActivity(Element initElement){//collect activity data from xml
    Activity act = new Activity();
    act.setName(initElement.getElementsByTagName("Name").item(0).getChildNodes().item(0).getNodeValue());
    act.OriginalDuration = Integer.parseInt(initElement.getElementsByTagName("PlannedDuration").item(0).getChildNodes().item(0).getNodeValue());
    act.RemainingDuration = Integer.parseInt(initElement.getElementsByTagName("RemainingDuration").item(0).getChildNodes().item(0).getNodeValue());
    act.ActualDuration = Integer.parseInt(initElement.getElementsByTagName("ActualDuration").item(0).getChildNodes().item(0).getNodeValue());

    act.EACDuration = act.RemainingDuration + act.ActualDuration;
    if(act.EACDuration>act.OriginalDuration)act.ActivitySlippage = true;
    else act.ActivitySlippage = false;
    if(act.EACDuration<=act.OriginalDuration)act.ActivityPerformedWell = true;
    else act.ActivityPerformedWell = false;

    //Problem============================================================
    act.RemainingLateFinishDate  = new DateTime(initElement.getElementsByTagName("RemainingLateFinishDate").item(0).getChildNodes().item(0).getNodeValue());
    act.RemainingEarlyFinishDate = new DateTime(initElement.getElementsByTagName("RemainingEarlyFinishDate").item(0).getChildNodes().item(0).getNodeValue());
    act.PlannedFinishDate = new DateTime(initElement.getElementsByTagName("PlannedFinishDate").item(0).getChildNodes().item(0).getNodeValue());
    //===================================================================

    act.Float1 = new Duration(act.RemainingEarlyFinishDate,act.RemainingLateFinishDate);
    act.Float2 = new Duration(act.PlannedFinishDate,act.RemainingLateFinishDate);
    if(act.Float1.isLongerThan(act.Float2))act.TotalFloat = act.Float2;
    else act.TotalFloat = act.Float1;

    if(act.TotalFloat.isShorterThan(Duration.ZERO)||act.TotalFloat.isEqual(Duration.ZERO))act.CriticalActivity = true;
    if(act.TotalFloat.isShorterThan(Duration.ZERO))act.NegativeFloatActivity = true;

    return act;
}

我无法包含XML文件,因为它真的很长。如果您希望我在某处发布,请表达您的担忧。但是,你可以说我所引用的所有元素都在正确的节点下面。

谢谢!

编辑(某些XML)

<Activity>
                <ActualDuration>0</ActualDuration>
                <ActualFinishDate xsi:nil="true" />
                <ActualLaborCost>0</ActualLaborCost>
                <ActualLaborUnits>0</ActualLaborUnits>
                <ActualNonLaborCost>0</ActualNonLaborCost>
                <ActualNonLaborUnits>0</ActualNonLaborUnits>
                <ActualStartDate xsi:nil="true" />
                <ActualThisPeriodLaborCost>0</ActualThisPeriodLaborCost>
                <ActualThisPeriodLaborUnits>0</ActualThisPeriodLaborUnits>
                <ActualThisPeriodNonLaborCost>0</ActualThisPeriodNonLaborCost>
                <ActualThisPeriodNonLaborUnits>0</ActualThisPeriodNonLaborUnits>
                <AtCompletionDuration>1104</AtCompletionDuration>
                <AtCompletionLaborCost>470304</AtCompletionLaborCost>
                <AtCompletionLaborUnits>1104</AtCompletionLaborUnits>
                <AtCompletionNonLaborCost>93840</AtCompletionNonLaborCost>
                <AtCompletionNonLaborUnits>1104</AtCompletionNonLaborUnits>
                <AutoComputeActuals>1</AutoComputeActuals>
                <CalendarObjectId>597</CalendarObjectId>
                <DurationPercentComplete>0</DurationPercentComplete>
                <DurationType>1</DurationType>
                <EstimatedWeight>1</EstimatedWeight>
                <ExpectedFinishDate xsi:nil="true" />
                <ExternalEarlyStartDate xsi:nil="true" />
                <ExternalLateFinishDate xsi:nil="true" />
                <Feedback xsi:nil="true" />
                <FinishDate>2008-05-22T17:00:00</FinishDate>
                <GUID>{841EDB12-3616-0043-BE5D-58C14151D45D}</GUID>
                <Id>LH750</Id>
                <IsNewFeedback>0</IsNewFeedback>
                <LevelingPriority>2</LevelingPriority>
                <Name>Elevator</Name>
                <NonLaborUnitsPercentComplete>0</NonLaborUnitsPercentComplete>
                <NotesToResources xsi:nil="true" />
                <ObjectId>43877</ObjectId>
                <PercentComplete>0</PercentComplete>
                <PercentCompleteType>1</PercentCompleteType>
                <PlannedDuration>1104</PlannedDuration>
                <PlannedFinishDate>2008-05-22T17:00:00</PlannedFinishDate>
                <PlannedLaborCost>470304</PlannedLaborCost>
                <PlannedLaborUnits>1104</PlannedLaborUnits>
                <PlannedNonLaborCost>93840</PlannedNonLaborCost>
                <PlannedNonLaborUnits>1104</PlannedNonLaborUnits>
                <PlannedStartDate>2007-11-13T08:00:00</PlannedStartDate>
                <PrimaryConstraintDate xsi:nil="true" />
                <PrimaryConstraintType xsi:nil="true" />
                <PrimaryResourceObjectId>1716</PrimaryResourceObjectId>
                <ProjectObjectId>462</ProjectObjectId>
                <RemainingDuration>1104</RemainingDuration>
                <RemainingEarlyFinishDate>2008-05-22T17:00:00</RemainingEarlyFinishDate>
                <RemainingEarlyStartDate>2007-11-13T08:00:00</RemainingEarlyStartDate>
                <RemainingLaborCost>470304</RemainingLaborCost>
                <RemainingLaborUnits>1104</RemainingLaborUnits>
                <RemainingLateFinishDate>2008-06-19T17:00:00</RemainingLateFinishDate>
                <RemainingLateStartDate>2008-02-29T08:00:00</RemainingLateStartDate>
                <RemainingNonLaborCost>93840</RemainingNonLaborCost>
                <RemainingNonLaborUnits>1104</RemainingNonLaborUnits>
                <ResumeDate xsi:nil="true" />
                <SecondaryConstraintDate xsi:nil="true" />
                <SecondaryConstraintType xsi:nil="true" />
                <StartDate>2007-11-13T08:00:00</StartDate>
                <Status>0</Status>
                <SuspendDate xsi:nil="true" />
                <Type>2</Type>
                <UnitsPercentComplete>0</UnitsPercentComplete>
                <WBSObjectId>4364</WBSObjectId>


                <....... />

            </Activity>

1 个答案:

答案 0 :(得分:1)

我不知道您正在使用哪个DOM库,但如果修改代码会发生什么:

act.RemainingLateFinishDate  = new DateTime(initElement.getElementsByTagName("RemainingLateFinishDate").getNodeValue());