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

The Archive Base Latest Questions

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

I am parsing data from a web service shown below: I am only able

  • 0

I am parsing data from a web service shown below: I am only able to parse two values that are status and bal and coming next to that i am getting null pointer exception.
I am able to retrieve only status and lname.Can anyone tell me what is the problem with this. I am getting
status = 1 which is correct, bal = Finn, and rest are null.

XML:

<root>
<response>
<status>1</status>
<bal>3.69 EUR</bal>
<cur>EUR</cur>
<acc>2974342749</acc>
<fname>Welcome To</fname>
<lname>Finn</lname>
</response>
</root>

Code:

Main Activity

btn.setOnClickListener(new OnClickListener() {

            public void onClick(View v) {
                new Communicator().execute();
            }
        });
}

public class Communicator extends AsyncTask<String, String, SitesList1>
{
    @Override
        protected SitesList1 doInBackground(String... params) {
            try {
                    SAXParserFactory spf = SAXParserFactory.newInstance();
                    SAXParser sp = spf.newSAXParser();
                    XMLReader xr = sp.getXMLReader();

                    URL sourceUrl = new URL("XXXXX![enter image description here][2]");
                    MyXMLHandler1 myXMLHandler1 = new MyXMLHandler1();
                    xr.setContentHandler(myXMLHandler1);
                    xr.parse(new InputSource(sourceUrl.openStream()));
            } catch (Exception e) {
                System.out.println("XML Pasing Excpetion = " + e);
            }

            sitesList1 = MyXMLHandler1.sitesList1;

            return sitesList1;
        }

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
        }

        @SuppressWarnings("deprecation")
        @Override
        protected void onPostExecute(SitesList1 result) 
        {
                status = sitesList1.getStatus().toString();
                System.out.println("status"+status);
                bal = sitesList1.getBal().toString();
                System.out.println("bal"+bal);
                cur = sitesList1.getCur().toString();
                System.out.println("cur"+cur);
                acc = sitesList1.getAcc().toString();
                System.out.println("acc"+acc);
                fname = sitesList1.getFname().toString();
                System.out.println("fname"+fname);
                lname = sitesList1.getLname().toString();
                System.out.println("lname"+lname);

        }
    }

Handler:

public class MyXMLHandler1 extends DefaultHandler{

Boolean currentElement = false;
String currentValue = null;
public static SitesList1 sitesList1 = null;

public  SitesList1 getSitesList1() {
    return sitesList1;
}

public  void setSitesList1(SitesList1 sitesList1) {
    this.sitesList1 = sitesList1;
}

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

    currentElement = true;

    if (localName.equals("root")) {
        sitesList1 = new SitesList1();
    }
}

@Override
public void endElement(String uri, String localName, String qName)
        throws SAXException {

    currentElement = false;

    if (localName.equalsIgnoreCase("status"))
        sitesList1.setStatus(currentValue);
    else if (localName.equalsIgnoreCase("bal"))
        sitesList1.setBal(currentValue);
    else if (localName.equalsIgnoreCase("cur"))
        sitesList1.setBal(currentValue);
    else if (localName.equalsIgnoreCase("acc"))
        sitesList1.setBal(currentValue);
    else if (localName.equalsIgnoreCase("fname"))
        sitesList1.setBal(currentValue);
    else if (localName.equalsIgnoreCase("lname"))
        sitesList1.setBal(currentValue);

}

@Override
public void characters(char[] ch, int start, int length)
        throws SAXException {

    if (currentElement) {
        currentValue = new String(ch, start, length);
        currentElement = false;
    }

}

}

Setter Getter:

public class SitesList1 {

private String status;
private String bal;
private String cur;
private String acc;
private String fname;
private String lname;
public String getStatus() {
    return status;
}
public void setStatus(String status) {
    this.status = status;
}
public String getBal() {
    return bal;
}
public void setBal(String bal) {
    this.bal = bal;
}
public String getCur() {
    return cur;
}
public void setCur(String cur) {
    this.cur = cur;
}
public String getAcc() {
    return acc;
}
public void setAcc(String acc) {
    this.acc = acc;
}
public String getFname() {
    return fname;
}
public void setFname(String fname) {
    this.fname = fname;
}
public String getLname() {
    return lname;
}
public void setLname(String lname) {
    this.lname = lname;
}

}

  • 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:56:15+00:00Added an answer on June 13, 2026 at 9:56 am

    The problem is in your characters method. You’re not resetting currentValue correctly. Try something like this instead:

    public void characters(char[] ch, int start, int length)
                throws SAXException {
      super.characters(ch, start, length);
      currentValue.append(ch, start, length);
    }
    
    public void startDocument() throws SAXException {
    super.startDocument();
    currentValue= new StringBuilder();
    }
    

    Then, in your endElement method, you can do this:

    public void endElement(String uri, String localName, String qName)
            throws SAXException {
    
        currentElement = false;
    
        if (localName.equalsIgnoreCase("status"))
            sitesList1.setStatus(currentValue.toString());
        else if (localName.equalsIgnoreCase("bal"))
            sitesList1.setBal(currentValue.toString());
        else if (localName.equalsIgnoreCase("cur"))
            sitesList1.setCur(currentValue.toString());
        else if (localName.equalsIgnoreCase("acc"))
            sitesList1.setAcc(currentValue.toString());
        else if (localName.equalsIgnoreCase("fname"))
            sitesList1.setFname(currentValue.toString());
        else if (localName.equalsIgnoreCase("lname"))
            sitesList1.setLname(currentValue.toString());
    
       //flush out the StringBuilder so you can start again
       curentValue.setLength(0);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm working on a project that parse data from web service and shows in
i am using NSXMLParser to parse xml data received from a web service in
I currently have a script parsing data from a Web Service, all examples I've
I have a app that parses all my data from my web service as
I have a web service class that successfully pulls my JSON from a feed
I'm using SimpleXML to get pieces of data from an XML web-service response. We
I have two threads: Thread 1 is fetching XML from a web service in
I'm trying to grab some JSON data from a web service using JSON.Net. The
I need to get Json data from a C# web service. I know there
In my project I am consuming web services and parsing data from web services

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.