1

I am parsing the following XML:

<?xml version="1.0"?>
<Root>
    <ResponseCode>1</ResponseCode>
    <ResponseMessage>Login Successfull</ResponseMessage>
    <ResultSet>
        <User>
            <id>3</id>
            <username>hassan</username>
            <email>hassan</email>
            <password>abcd</password>
            <profileimagepath>c:/hahaha/hahaha</profileimagepath>
            <createdOn>2013-03-23 12:45:51</createdOn>
            <status>1</status>
        </User>
    </ResultSet>
</Root>

The code I'm using to parse the XML is:

InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlResult));
XPathFactory factory = XPathFactory.newInstance();
XPath xPath = factory.newXPath();

try {
    NodeList list = (NodeList) xPath.evaluate("/Root", is,XPathConstants.NODESET);
    for(int i=0; i<list.getLength(); i++){
        Node node = list.item(i);
        Element element = (Element) node;
        Log.d("VALUE OF NODE", element.getNodeValue());
    }
} catch (XPathExpressionException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

I cannot judge why this exception occurs. What is the correct way to parse it? I am executing this on Android 4.0. Here is the logcat

03-24 01:50:54.412: I/Post service Response(1097): <?xml version="1.0"?><Root><ResponseCode>1</ResponseCode><ResponseMessage>Login Successfull</ResponseMessage><ResultSet><User><id>3</id><username>hassan</username><email>hassan</email><password>abcd</password><profileimagepath>c:/hahaha/hahaha</profileimagepath><createdOn>2013-03-23 12:45:51</createdOn><status>1</status></User></ResultSet></Root>
03-24 01:50:54.804: D/dalvikvm(1097): GC_CONCURRENT freed 1940K, 21% free 7888K/9927K, paused 11ms+15ms
03-24 01:50:55.284: W/dalvikvm(1097): threadid=11: thread exiting with uncaught exception (group=0x409961f8)
03-24 01:50:55.293: E/AndroidRuntime(1097): FATAL EXCEPTION: Thread-102
03-24 01:50:55.293: E/AndroidRuntime(1097): java.lang.NullPointerException: println needs a message
03-24 01:50:55.293: E/AndroidRuntime(1097):     at android.util.Log.println_native(Native Method)
03-24 01:50:55.293: E/AndroidRuntime(1097):     at android.util.Log.d(Log.java:138)
03-24 01:50:55.293: E/AndroidRuntime(1097):     at com.teamgreen.greenit.LoginActivity$1$1.run(LoginActivity.java:87)
03-24 01:51:28.253: I/Process(1097): Sending signal. PID: 1097 SIG: 9
1
  • add here logcat, please. Commented Mar 23, 2013 at 20:49

1 Answer 1

2

You're getting an exception from inside Log.d(), because element.getNodeValue() is returning null.

You cannot provide null as the second parameter into Log.d(). Consider changing the respective line to:

Log.d("xml", "VALUE OF NODE: " + element.getNodeValue());

Instead of focusing on the specific problem, you should instead try to understand interpreting stack traces. The stack trace points to the exact problem:

E/AndroidRuntime(1097): at android.util.Log.d(Log.java:138)
E/AndroidRuntime(1097): at com.teamgreen.greenit.LoginActivity$1$1.run(LoginActivity.java:87)

Sign up to request clarification or add additional context in comments.

4 Comments

well this solves the exception, but I cannot get the inner data, how do I do that?
Have you found a resolution to your problem?
How can we check the tag contains null value?
I'm not sure I fully understand your question, but you can simply check for null using element.getNodeValue() == null

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.