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

  • SEARCH
  • Home
  • 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 4586668
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T21:40:16+00:00 2026-05-21T21:40:16+00:00

In my app I’m parsing an xml, piece of structure doing problems: <answers> <answer

  • 0

In my app I’m parsing an xml, piece of structure doing problems:

<answers>
    <answer value="A">A</answer>
    <answer value="B">B</answer>
    <answer value="C">C</answer>
</answers>

I’m parsing it with XML DOM:

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();

that works great and depending on answer items I’m creating a RadioButtons like this:

NodeList answers = doc.getElementsByTagName("answers").item(0).getChildNodes();

int j = 0;
RadioGroup group = new RadioGroup(this);
RadioButton button1 = new RadioButton(this);
button1.setId((i+1)*100+(j++));
button1.setText(answers.item(1).getChildNodes().item(0).getNodeValue());
button1.setTextColor(Color.BLACK);

RadioButton button2 = new RadioButton(this);
button2.setId((i+1)*100+(j++));
button2.setText(answers.item(2).getChildNodes().item(0).getNodeValue());
button2.setTextColor(Color.BLACK);

RadioButton button3 = new RadioButton(this);
button3.setId((i+1)*100+(j));
button3.setText(answers.item(3).getChildNodes().item(0).getNodeValue());
button3.setTextColor(Color.BLACK);

This piece of code works perfectly in the emulator, SDK v.7 (Android 2.0), while my HTC Desire runs on Android 2.1u1 (so SDK v.8)

But in the device I get error on this line button2.setText(answers.item(2).getChildNodes().item(0).getNodeValue()); guessing that there is no .item(2) in answers – but it has to be… I was debugging this code within emulator and found out that answers.item(0) is a TextNode containing the name of the XML node “answers”…

But it is true I’m a bit confused and everything is messing up when parsing this XML as I still have to count how deep am I and when to call what index on which element (node)… But still I found this implementation much simpler than using SAX…

Isn’t there something simillar to SimpleXml from PHP in Java???

Anyway, my main problem is: how is it possible that application is working perfectly in emulator while on device it throws NullPointerException on the line where I try to set text for button2???

Many thanks for You help!!!

  • 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-21T21:40:17+00:00Added an answer on May 21, 2026 at 9:40 pm

    getChildNodes() returns all Nodes under answers, not just all Elements. You probably want to iterate through all of the children and check if each is an Element with tag name “answer”

    How about something like this:

    NodeList answers = doc.getElementsByTagName("answer");
    for (int x = 0; x < answers.getLength(); x++) {
      Node answer = answers.get(x);
      // you could do some checking here, make sure Node is instanceof Element, etc.
      RadioButton radioButton = new RadioButton(this);
      radioButton.setId((i+1)*100+(x));
      radioButton.setText(node.getNodeValue());
      radioButton.setTextColor(Color.BLACK);
      // add the radio button to some view
    }
    

    That way you aren’t dependent on a particular number of child elements of answers, and you guarantee you won’t try to access one that doesn’t exist.

    If you really want to do it based on the children of an Answers node,

    Node answersNode = // get the node
    NodeList childNodes = answersNode.getChildNodes();
    for (int x = 0; x < childNodes.getLength(); x++) {
      Node node = childNodes.get(0);
      if (node instanceof Element && node.getNodeName().equals("answer")) {
        // do the same as above to create a radio button.
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

App.config: <?xml version=1.0 encoding=utf-8 ?> <configuration> <appSettings> <add key=1 value=An error occured.\r\nPlease try again
app.get('/', function (req, res) { res.render('home.jade', { results: req.session.value }); }); What i would
App.config: <add key=SaveDraftPath value=C:\Drafts\/> C#: var saveDraftPath = ConfigurationManager.AppSettings[SaveDraftPath]; var sfDialog = new SaveFileDialog();
My app is working on many devices without problems so far. But now I
App Engine only allows you to use these formats for XMPP addresses: app-id@appspot.com anything@app-id.appspotchat.com
My app is expanding, but the reqs dictate that there should be only one
Our app uses a component that requires a license file in the directory with
My app consists of 3 views. I've defined 3 helper methods in the AppDelegate
My app is made up of a TabBarController, each tab with a UITableView. On
My app was built with SDK 2.2.1 version even before 3.0 beta appear. User

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.