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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T03:58:09+00:00 2026-06-08T03:58:09+00:00

here i am just trying to parse an xml file and store the contents

  • 0

here i am just trying to parse an xml file and store the contents in an array lists, but everytime its giving java.Lang.NullPointerException at two points, pls help in debugging this…

public class XML_PARSER extends Activity {

String TAG= "XML_PARSER";
List optionList = new ArrayList(); 
Document dom;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.parser);



    Document doc=parseXmlFile();

at this line it is giving a java null pointer exception

    ParseDocument(doc); 

    //printData();


}
private Document parseXmlFile(){
    DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();

    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        dom=db.parse("R.raw.options.xml");

    }

    catch(ParserConfigurationException pce){
        pce.printStackTrace();
    }
    catch(SAXException se){
        se.printStackTrace();
    }
    catch(IOException ioe){
        ioe.printStackTrace();
    }
    return dom;

}


private void ParseDocument(Document dom){

at this lineits giving the same error

    Element docEle = dom.getDocumentElement();
    Node node;

    NodeList n1= docEle.getElementsByTagName("Option");
    if(n1!=null && n1.getLength()>0){
        for(int i=0;i<n1.getLength();i++){
            node=n1.item(i);

            Element e1=(Element)n1.item(i);

            Option e = getOption(e1,node);

            optionList.add(e);

            }

        }
    }

here is the log cat for the same

07-20 12:21:48.391: E/AndroidRuntime(836): Caused by: java.lang.NullPointerException
07-20 12:21:48.391: E/AndroidRuntime(836):  at com.example.xml_parser.XML_PARSER.ParseDocument(XML_PARSER.java:67)

07-20 12:21:48.391: E/AndroidRuntime(836):  at com.example.xml_parser.XML_PARSER.onCreate(XML_PARSER.java:38)

07-20 12:21:48.391: E/AndroidRuntime(836):  at android.app.Activity.performCreate(Activity.java:5008)

07-20 12:21:48.391: E/AndroidRuntime(836):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)

07-20 12:21:48.391: E/AndroidRuntime(836):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
  • 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-08T03:58:12+00:00Added an answer on June 8, 2026 at 3:58 am

    The problem is that R.raw.options.xml is not a valid URI so in this line dom=db.parse("R.raw.options.xml");, the dom variable is null. You are trying to use methods of the null variable doc (line Element docEle = dom.getDocumentElement(); inside the ParseDocument(doc); method), that is why you get a NullPointerException. If you pass a valid URI in dom=db.parse("valid_URI"); method you will eliminate the twoNullPointerExceptions.

    EDIT Here is a complete example:

    private Document parseXmlFile(){
        DocumentBuilderFactory dbf= DocumentBuilderFactory.newInstance();
    
        try {
           DocumentBuilder db = dbf.newDocumentBuilder();
           Resources res = getResources();
           InputStream in = res.openRawResource(R.raw.options);
           dom=db.parse(in); //Here instead of passing the URI string, you pass the input stream of the file.
    
        }
    
        catch(ParserConfigurationException pce){
            pce.printStackTrace();
        }
        catch(SAXException se){
            se.printStackTrace();
        }
        catch(IOException ioe){
            ioe.printStackTrace();
        }
        return dom;
    
    }
    

    Hope that helps.

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

Sidebar

Related Questions

Here is a snippet from the XML file I am trying to parse: <Item
I am just trying to fill my ListView but I get a NullPointerException Here
This might sound like a stupid question but just trying to learn something here.
Ok, Stupid question here... just trying to do a search in an array to
I've been trying to grab the Digg rss feed and parse its contents into
I am trying to parse a XML document using Xerces, but I cant seem
I am trying to parse a bit of data from an HTML file, but
I am trying to parse this HTML page here with HTML Agility Pack, but
I'm trying to parse a massive xml file into my MySQL database. the file
A quick question here: is it faster to load a large XML file (just

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.