我在android中解析XML时遇到异常

时间:2012-11-22 17:01:18

标签: android xml xml-parsing

代码是

    ///  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

引发例外是休闲

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

1 个答案:

答案 0 :(得分:0)

如果以下行导致NullPointerException,则childlist可能为null。

String s = childlist.getNodeValue();

如果以下调用返回null,则对象子列表为null。

childlist = n.getFirstChild();

如果n指向的相应xml节点中没有子节点,则Node类的方法getFirstChild()将返回null。比较documentation of getFirstChild。因此,您应该检查n指向哪个xml节点,并且该节点是否具有子节点。为了获得更好的答案,您必须举例说明您正在解析的xml文档。