0

I have sale tag as child node that exists for multiple times but in that Employee tag doesn't exists for all sale tag.need to parse this xml.Got Null pointer exception when there is no employee tag.How to parse this xml even if some child tag name doesn't exists?

<?xml version="1.0" encoding="ISO-8859-1"?>
   <Transactions>
      <DocumentInfo TransferType="Transactions" Copyright="2010, Hectronic GmbH" CreatedBy="HecPoll">
       <CreateDate Year="2011" Month="7" Day="13"/>
       <CreateTime Hour="6" Minute="0" Second="16"/>
      </DocumentInfo>
      <DocumentData>
      <Sale RcpNum="151" Amount="228.760" Station="1" Customer="000003" Terminal="1">
        <Date Year="2011" Month="07" Day="12"/>
        <Time Hour="07" Minute="23" Second="00"/>
        <ArticleSale Quantity="6.020" SglPriceAsSold="38.000" SglPriceAsStored="38.000" SglPrice="38.000" Amount="228.760" FCCAmount="228.760" Taxrate="18" LogicalFPNumber="1" TankNumber="3" ArtNum="3" SaleNum="1"/>
        <Payment PAN="000000002117904490D" Amount="228.760" TenderCode="0" PayNum="1"/>
        <Employee Lastname="V KALYANI"/>
        <Vehicle LicensePlate="10K"/>
      </Sale>
      <Sale RcpNum="255" Amount="6650.000" Station="1" Customer="ZZZZZZ" Terminal="0">
        <Date Year="2011" Month="07" Day="12"/>
        <Time Hour="07" Minute="24" Second="00"/>
        <ArticleSale Quantity="175.000" SglPriceAsSold="38.000" SglPriceAsStored="38.000" SglPrice="38.000" Amount="6650.000" FCCAmount="6650.000" Taxrate="19" LogicalFPNumber="1" TankNumber="1" ArtNum="3" SaleNum="1"/>
        <Payment PAN="000000001885018677D" Amount="6650.000" TenderCode="0" PayNum="1"/>
      </Sale>
      <Sale RcpNum="152" Amount="3078.000" Station="1" Customer="000003" Terminal="1">
        <Date Year="2011" Month="07" Day="12"/>
        <Time Hour="07" Minute="27" Second="00"/>
        <ArticleSale Quantity="81.000" SglPriceAsSold="38.000" SglPriceAsStored="38.000" SglPrice="38.000" Amount="3078.000" FCCAmount="3078.000" Taxrate="18" LogicalFPNumber="1" TankNumber="3" ArtNum="3" SaleNum="1"/>
        <Payment PAN="000000002117928224D" Amount="3078.000" TenderCode="0" PayNum="1"/>
        <Employee Lastname="A MUNIYANDI"/>
        <Vehicle LicensePlate="10F"/>
      </Sale>
   </DocumentData>
</Transactions>

Thanks,

3
  • can you pls show us your XML? Commented Mar 28, 2013 at 6:35
  • 1
    Show the XML snippet and the Java code where the NullPointerException occurs. Commented Mar 28, 2013 at 6:38
  • U can try to generate an xsd for it(manually or automaticly). with that, u can make it automaticly and ignore null values also instead of handling exceptions. Commented Mar 28, 2013 at 9:19

1 Answer 1

2

I assume you are using dom parser. If you are using getElementsByTagName() method, it will return null if no such element found. You better check for null for Employee tag.

Sign up to request clarification or add additional context in comments.

2 Comments

how to check null for the employee tag?
NodeList n = e.getElementsByTagName(name); // here e refers to Sale node if(NodeList != null ) // do whatever you want

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.