Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7879395
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T03:53:28+00:00 2026-06-03T03:53:28+00:00

I am using the Document object to extract all the tags from an xml.

  • 0

I am using the Document object to extract all the tags from an xml. If the xml has an empty tag, I get a null pointer exception. How do I guard against this? How do I check for an empty tag?

<USTrade>
<CreditorId>
<CustomerNumber>xxxx</CustomerNumber>
<Name></Name>
<Industry code="FY" description="Factor"/>
</CreditorId>
<DateReported format="MM/CCYY">02/2012</DateReported>
<AccountNumber>54000</AccountNumber>
<HighCreditAmount>0000299</HighCreditAmount>
<BalanceAmount>0000069</BalanceAmount>
<PastDueAmount>0000069</PastDueAmount>
<PortfolioType code="O" description="Open Account (30, 60, or 90 day account)"/>
<Status code="5" description="120 Dys or More PDue"/>
 <Narratives>
<Narrative code="GS" description="Medical"/>
<Narrative code="CZ" description="Collection Account"/>
</Narratives>
</USTrade>
<USTrade>

So, when I use:

                NodeList nm = docElement.getElementsByTagName("Name");
                if (nm.getLength() > 0)
                    name = nullIfBlank(((Element) nm.item(0))
                            .getFirstChild().getTextContent());

Nodelist gives a length of 1, because there is a tag, but when I do getTextContent(), it hits the null pointer because FirstChild() doesn’t return anything for tag = Name

And, I have done this for each xml tag. Is there a simple check I can do before every tag extraction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T03:53:30+00:00Added an answer on June 3, 2026 at 3:53 am

    The first thing I would do would be to unchain your calls. This will give you the chance to determine exactly which reference is null and which reference you need to do a null check for:

     NodeList nm = docElement.getElementsByTagName("Name");
                    if (nm.getLength() > 0) {
                        Node n = nm.item(0);
                        Node child = n.getFirstChild();
                        if(child == null) {
                            // null handling
                            name = null;
                         }
                        else {
                           name = nullIfBlank(child.getTextContent());
                        }
    
                     }
    

    Also, check out the hasChildNodes() method on Node! http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#hasChildNodes%28%29

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to extract information from an XML file into an object using
I have a few .Net projects that would benefit from using a document/object database
By using document.referrer we will get all the reference of URL in JavaScript, such
I'm using Java's DocumentBuilder.parse(InputStream) to parse an XML document. Occasionally, I get malformed XML
Using PHP, how do I get an entire subset of nodes from an XML
I'm reading xml document using XPath and I need to create a Document object
I need to create a XML Document object using the NodeList. Can someone pls
I would like to extract all img tags that are within an anchor tag
I want to know how can I extract part of an XML document using
How can I access window.document object from ActiveXObject in InternetExplorer? I'm using C# language

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.