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 6214311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T06:51:15+00:00 2026-05-24T06:51:15+00:00

I am parsing the xml file. I am always getting NullPointerException . Can anyone

  • 0

I am parsing the xml file. I am always getting NullPointerException. Can anyone suggest me where I made a mistake?

<?xml version="1.0"?>
 <categories>
<category name="ABC">
    <subcategory name="windows" 
        loc="C://program files" 
        link="www.sample.com" 
        parentnode="Mac"/>
    <subcategory name="456" 
        loc="C://program files" 
        link="http://" 
        parentnode="ABC"/>
</category>

    <category name="XYZ"> 
        <subcategory name="android" 
            loc="C://program files" 
            link="www.sample.com" 
            parentnode="XYZ"/>
        <subcategory name="apple" 
            loc="C://program files" 
            link="http://abc.com" 
            parentnode="XYZ"/>
    </category>
</categories>

In the above xml file, I want to parse only the subcategory name android. For this, I made

NodeList catLst = doc.getElementsByTagName("category");

            for (int i = 0; i < catLst.getLength(); i++) {

                Node cat = catLst.item(i);

                NamedNodeMap catAttrMap = cat.getAttributes();
                Node catAttr = catAttrMap.getNamedItem("name");

                if (catName.equals(catAttr.getNodeValue())) { // CLUE!!!

                    NodeList subcatLst = cat.getChildNodes();

                    for (int j = 0; j < subcatLst.getLength(); j++) {
                        Node subcat = subcatLst.item(j);
                        NamedNodeMap subcatAttrMap = subcat.getAttributes();
                        Node subCatAttr = subcatAttrMap.getNamedItem("name");

                        if (subCatfound.equals(subCatAttr.getNodeValue())
                                && subcatAttrMap != null) {
                            Node subcatAttr = subcatAttrMap.getNamedItem(attrName);
                            list.add(subcatAttr.getNodeValue());
                        } else {
                            System.out.println("NULL");
                        }
                    }
                }

Whenever I do this, I am getting NullPointerException. Can anyone know where I made a mistake?

  • 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-05-24T06:51:16+00:00Added an answer on May 24, 2026 at 6:51 am

    This code is a simplification of what you’re trying to trying to achieve:

    public static Element getElementByNameAttribute(String elementName, String nameAttributeValue, Document doc) {
        if (elementName!= null && !elementName.isEmpty() && nameAttributeValue!= null && !nameAttributeValue.isEmpty()) {
    
            NodeList subCategoryList = doc.getElementsByTagName(elementName);
            for (int i = 0; i < subCategoryList.getLength(); i++) {
                Element element = (Element) subCategoryList.item(i);
    
                if (nameAttributeValue.equals(element.getAttribute("name"))) {
                    return element;
                }
            }
        }
    
        return null;
    }
    

    If you put this in a class, e.g. DOMUtil (in my case), you can simply do this:

    Element subCategoryAndroid = DOMUtil.getElementByNameAttribute("subcategory", "android", doc);
    

    PS: This is untested.

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

Sidebar

Related Questions

Can anyone share experience with parsing XML chunks embedded in a non-XML file? I
Possible Duplicate: Parsing XML in Cocoa iPhone - Can we parse .plist(xml) file using
I'm getting EXC_BAD_ACCESS while parsing XML file in my Xcode4 iPhone app Although I
I'm parsing big XML file with XPathExpression selection for some nodes existing at various
I am parsing a XML file in Java using the W3C DOM. I am
I'm parsing an xml file and i've been trying to strip out the whitespace
What is the fastest method of parsing an XML file in C#? I'm using
I got a string from parsing a XML file which looks like this: Fri,
I am using feedparser for parsing from XML file.But I couldn't parse <geo:lat> ,
Looking for something similar to xerces for parsing an xml file in ruby. I

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.