the code is
/// Element rootelement = doc.getDocumentElement();
XPathFactory xpathfactory = XPathFactory.newInstance();
XPath xpath = xpathfactory.newXPath();
try {
xpathexpression = xpath.compile("//@*[name()='diffgr:id']");
result = xpathexpression.evaluate(doc,XPathConstants.NODESET);
Log.v(result.toString(), "Value of result");
} catch (XPathExpressionException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
NodeList nodelist =(NodeList) result;
Node childlist = null;
// org.w3c.dom.Element rootelement = doc.getDocumentElement();
// NodeList nl = rootelement.getChildNodes();
Log.v(new Integer(nodelist.getLength()).toString(), "Lenght of node list");
for (int i =0; i< nodelist.getLength(); i++)
{
Node n = nodelist.item(i);
System.out.println("message");
childlist = n.getFirstChild();
System.out.println("message2");
String s = childlist.getNodeValue(); // THIS LINE GIVES EXCEPTION ELSE CODE IS CORRECT
THe exception is raised is as fallow
11-22 21:50:58.166: E/AndroidRuntime(1123): FATAL EXCEPTION: main
11-22 21:50:58.166: E/AndroidRuntime(1123): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.axml/com.example.axml.MainActivity}: java.lang.NullPointerException
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.os.Handler.dispatchMessage(Handler.java:99)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.os.Looper.loop(Looper.java:123)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.app.ActivityThread.main(ActivityThread.java:4627)
11-22 21:50:58.166: E/AndroidRuntime(1123): at java.lang.reflect.Method.invokeNative(Native Method)
11-22 21:50:58.166: E/AndroidRuntime(1123): at java.lang.reflect.Method.invoke(Method.java:521)
11-22 21:50:58.166: E/AndroidRuntime(1123): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-22 21:50:58.166: E/AndroidRuntime(1123): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-22 21:50:58.166: E/AndroidRuntime(1123): at dalvik.system.NativeStart.main(Native Method)
11-22 21:50:58.166: E/AndroidRuntime(1123): Caused by: java.lang.NullPointerException
11-22 21:50:58.166: E/AndroidRuntime(1123): at com.example.axml.MainActivity.onCreate(MainActivity.java:92)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-22 21:50:58.166: E/AndroidRuntime(1123): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2 627)
11-22 21:50:58.166: E/AndroidRuntime(1123): ... 11 more
11-22 21:50:58.186: W/ActivityManager(58): Force finishing activity com.example.axml/.MainActivity
11-22 21:50:58.697: W/ActivityManager(58): Activity pause timeout for HistoryRecord{44f68330 com.example.axml/.MainActivity}
11-22 21:51:05.305: D/dalvikvm(288): GC_EXPLICIT freed 29 objects / 1416 bytes in 131ms
If the following line causes the NullPointerException, then childlist is probably null.
String s = childlist.getNodeValue();
The object childlist is null if the following call returns null.
childlist = n.getFirstChild();
The method getFirstChild() of the class Node results in null if there are no childs in the corresponding xml node n is pointing to. Compare the documentation of getFirstChild . Therefore, you should check to which xml node n is pointing to and weather this node has childs or not. For a better answer you have to give an example of the xml document you are parsing I guess.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.