2012-11-22 58 views
1

解析XML代碼得到的例外是我在安卓

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

回答

0

如果下面的行導致空指針異常,那麼可能的childList爲空。

String s = childlist.getNodeValue(); 

如果以下調用返回null,則對象childlist爲null。

childlist = n.getFirstChild(); 

如果在指向的對應xml節點中沒有子節點,則類節點的getFirstChild()方法的結果爲null。比較documentation of getFirstChild。因此,您應該檢查哪個xml節點n指向哪個節點,並且該節點是否具有子節點。爲了得到更好的答案,你必須給出一個你正在解析的xml文檔的例子。