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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T09:09:43+00:00 2026-06-13T09:09:43+00:00

I created an app to parse a XML file using SAXParser and the code

  • 0

I created an app to parse a XML file using SAXParser and the code is below.

Below is the code of my main activity.

public class XMLAppActivity extends Activity{
 ArrayList<Question> questionList = new ArrayList<Question>();          
 TextView tvXmlReader;
 InputSource xmlSource;
 InputStream xmlIs;
 String xmlFile;


  @Override
  public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.main);
      tvXmlReader=(TextView)findViewById(R.id.my_xml);

      try 
        {
          xmlIs= getAssets().open("surveyquestions.xml");
          xmlFile = convertStreamToString(xmlIs);
        } 
        catch (IOException e1)
        {
            // TODO Auto-generated catch block
            e1.printStackTrace();
        } 

     xmlSource=  new InputSource(new StringReader(xmlFile));
      tvXmlReader.setText(parseXML().toString());
  }

 private String parseXML() {

        try {


            /** Handling XML */
            SAXParserFactory spf = SAXParserFactory.newInstance();
            SAXParser sp = spf.newSAXParser();
            XMLReader xr = sp.getXMLReader();

            QuestionXMLHandler myXMLHandler = new QuestionXMLHandler();
            xr.setContentHandler(myXMLHandler);

            xr.parse(xmlSource);

            questionList= myXMLHandler.getQuestionList();
            return "Success";

        }
        catch (Exception e) {
          return "Failure due to " + e.toString();
        }
   }

 public  String convertStreamToString(InputStream is)
            throws IOException {
            Writer writer = new StringWriter();

            char[] buffer = new char[2048];
            try {
                Reader reader = new BufferedReader(new InputStreamReader(is,"UTF-8"));
                int n;
                while ((n = reader.read(buffer)) != -1) {
                    writer.write(buffer, 0, n);
                }
            } finally {
                is.close();
            }
            String text = writer.toString();
            return text;
    }
}

This is the code for my XMLhandler.

public class QuestionXMLHandler extends DefaultHandler {

private ArrayList<Question> questionL= new ArrayList<Question>();
Question cQuestion;
Boolean cElement = false;
String cValue = "";


public ArrayList<Question> getQuestionList() {
    return questionL;
}


@Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {

    cElement = true;
    cValue = "";

    // if current element is Question , create new question
    // clear tmpValue on start of element

    if (qName.equalsIgnoreCase("Question")) {
        cQuestion = new Question();
        cQuestion.setQuestionId(attributes.getValue("id"));
        cQuestion.setQuestionName(attributes.getValue("name"));
        cQuestion.setQuestionType(attributes.getValue("type"));
        cQuestion.setQuestionIsRequired(Boolean.parseBoolean(attributes.getValue("isRequired")));
    }


}


@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {
      if (qName.equalsIgnoreCase("Question")) {
          questionL.add(cQuestion);
        }
        if (qName.equalsIgnoreCase("QuestionText")) {
            cQuestion.setQuestionText(cValue);
        }

        if(qName.equalsIgnoreCase("Answer")){
            cQuestion.getAnswers().add(cValue);
        }

}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {
    // TODO Auto-generated method stub
    cValue = new String(ch,start,length);
}
}

And This is my Question class.

public class Question 
{
private String qId;
private String qName;
private String qType;
private String qText;
private Boolean isRequired;
private String qSurveyId;
private List<String> answers;

public Question()
{
    qId="";
    qName="";
    qType="";
    qText="";
    isRequired=false;
    qSurveyId="";

}
 //getters and setter methods
}

But I m getting an error of:

06-27 14:44:36.148: W/System.err(591): java.lang.NullPointerException
06-27 14:44:36.187: W/System.err(591):  at com.optimus.mobile.xml.QuestionXMLHandler.endElement(QuestionXMLHandler.java:52)
06-27 14:44:36.187: W/System.err(591):  at org.apache.harmony.xml.ExpatParser.endElement(ExpatParser.java:160)
06-27 14:44:36.187: W/System.err(591):  at org.apache.harmony.xml.ExpatParser.append(Native Method)
06-27 14:44:36.187: W/System.err(591):  at org.apache.harmony.xml.ExpatParser.parseFragment(ExpatParser.java:505)
06-27 14:44:36.197: W/System.err(591):  at org.apache.harmony.xml.ExpatParser.parseDocument(ExpatParser.java:492)
06-27 14:44:36.197: W/System.err(591):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:308)
06-27 14:44:36.197: W/System.err(591):  at org.apache.harmony.xml.ExpatReader.parse(ExpatReader.java:264)
06-27 14:44:36.197: W/System.err(591):  at com.optimus.mobile.xml.XMLAppActivity.parseXML(XMLAppActivity.java:68)
06-27 14:44:36.197: W/System.err(591):  at com.optimus.mobile.xml.XMLAppActivity.onCreate(XMLAppActivity.java:52)
06-27 14:44:36.197: W/System.err(591):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-27 14:44:36.197: W/System.err(591):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-27 14:44:36.197: W/System.err(591):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-27 14:44:36.197: W/System.err(591):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-27 14:44:36.197: W/System.err(591):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-27 14:44:36.197: W/System.err(591):  at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 14:44:36.207: W/System.err(591):  at android.os.Looper.loop(Looper.java:123)
06-27 14:44:36.207: W/System.err(591):  at android.app.ActivityThread.main(ActivityThread.java:4627)
06-27 14:44:36.207: W/System.err(591):  at java.lang.reflect.Method.invokeNative(Native Method)
06-27 14:44:36.207: W/System.err(591):  at java.lang.reflect.Method.invoke(Method.java:521)
06-27 14:44:36.207: W/System.err(591):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
06-27 14:44:36.207: W/System.err(591):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
06-27 14:44:36.207: W/System.err(591):  at dalvik.system.NativeStart.main(Native Method)
  • 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-13T09:09:44+00:00Added an answer on June 13, 2026 at 9:09 am

    I spotted my mistake !! it was in the End Element method of my XML
    handler class. basically some problem in the below line.
    if(qName.equalsIgnoreCase(“Answer”)) {
    cQuestion.getAnswers().add(cValue); }

    worked for me

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

Sidebar

Related Questions

package com.parseador.prueba; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.EditText; public class main extends
I have created an app which retrieves the data from a url using xml
In my app I am trying to parse an XML file. I am able
I've created a simple XML-Parser to parse my rss Feeds to my app. The
I'm trying to learn how to parse an xml file using SAX parser in
I am using the following code by the Android-people-XMLParsing example for parsing XML file
So I just created a parser and a handler to parse an XML file.
I created an app in which a file is created. The file directory I
I created an App which uses Timer class to callback a method at a
I created an app for iPad (not using StoryBoards), with a UITabBarController. I added

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.