如何删除空指针异常

时间:2013-07-05 11:52:03

标签: android nullpointerexception

我正在尝试做xml解析程序.xml页面的链接是:http://api.androidhive.info/pizza/?format=xml

在进行http连接时,它显示错误:java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.xml / com.example.xml.XmlparseActivity}:java.lang.NullPointerException

然后我把Log消息放在每一行,所以问题出在HttpResponce没有日志消息显示在该行之后。解决问题。可以帮助我的程序代码如下所示:

 public String getXmlElement(String url) {
        // TODO Auto-generated method stub
        String xml=null;
        try{
        DefaultHttpClient client=new DefaultHttpClient();
        Log.d("cli", "ok");
        HttpPost post= new HttpPost(url);
        Log.d("post", "ok"+post);
        HttpResponse responce=client.execute(post);
        Log.d("responce", "ok");
        HttpEntity entity=responce.getEntity();
        Log.d("entity", "ok");
        xml=EntityUtils.toString(entity);
        }catch(UnsupportedEncodingException e){
            e.printStackTrace();
        }catch (IOException e) {
            // TODO: handle exception
            e.printStackTrace();
        }
        return xml;

    }

logcat页面是:

07-05 17:13:03.227: D/hai(809): http://api.androidhive.info/pizza/?format=xml
07-05 17:13:03.237: D/cli(809): ok
07-05 17:13:03.247: D/post(809): okorg.apache.http.client.methods.HttpPost@44ee99e8
07-05 17:13:03.317: W/System.err(809): java.net.UnknownHostException: api.androidhive.info
07-05 17:13:03.317: W/System.err(809):  at java.net.InetAddress.lookupHostByName(InetAddress.java:513)
07-05 17:13:03.327: W/System.err(809):  at java.net.InetAddress.getAllByNameImpl(InetAddress.java:278)
07-05 17:13:03.327: W/System.err(809):  at java.net.InetAddress.getAllByName(InetAddress.java:242)
07-05 17:13:03.327: W/System.err(809):  at org.apache.http.impl.conn.DefaultClientConnectionOperator.openConnection(DefaultClientConnectionOperator.java:136)
07-05 17:13:03.337: W/System.err(809):  at org.apache.http.impl.conn.AbstractPoolEntry.open(AbstractPoolEntry.java:164)
07-05 17:13:03.337: W/System.err(809):  at org.apache.http.impl.conn.AbstractPooledConnAdapter.open(AbstractPooledConnAdapter.java:119)
07-05 17:13:03.337: W/System.err(809):  at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:348)
07-05 17:13:03.347: W/System.err(809):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
07-05 17:13:03.347: W/System.err(809):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
07-05 17:13:03.347: W/System.err(809):  at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
07-05 17:13:03.357: W/System.err(809):  at com.example.xml.XmlParser.getXmlElement(XmlParser.java:93)
07-05 17:13:03.357: W/System.err(809):  at com.example.xml.XmlparseActivity.onCreate(XmlparseActivity.java:33)
07-05 17:13:03.357: W/System.err(809):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
07-05 17:13:03.357: W/System.err(809):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
07-05 17:13:03.357: W/System.err(809):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 17:13:03.367: W/System.err(809):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 17:13:03.367: W/System.err(809):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 17:13:03.367: W/System.err(809):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:13:03.367: W/System.err(809):  at android.os.Looper.loop(Looper.java:123)
07-05 17:13:03.377: W/System.err(809):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 17:13:03.377: W/System.err(809):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:13:03.377: W/System.err(809):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 17:13:03.377: W/System.err(809):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 17:13:03.388: W/System.err(809):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
07-05 17:13:03.388: W/System.err(809):  at dalvik.system.NativeStart.main(Native Method)
07-05 17:13:03.407: D/AndroidRuntime(809): Shutting down VM
07-05 17:13:03.407: W/dalvikvm(809): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
07-05 17:13:03.427: E/AndroidRuntime(809): FATAL EXCEPTION: main
07-05 17:13:03.427: E/AndroidRuntime(809): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.xml/com.example.xml.XmlparseActivity}: java.lang.NullPointerException
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.os.Looper.loop(Looper.java:123)
07-05 17:13:03.427: E/AndroidRuntime(809):  at android.app.ActivityThread.main(ActivityThread.java:4627)
07-05 17:13:03.427: E/AndroidRuntime(809):  at java.lang.reflect.Method.invokeNative(Native Method)
07-05 17:13:03.427: E/AndroidRuntime(809):  at java.lang.reflect.Method.invoke(Method.java:521)
07-05 17:13:03.427: E/AndroidRuntime(809):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
07-05 17:13:03.427: E/AndroidRuntime(809):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)

3 个答案:

答案 0 :(得分:1)

您是否在清单中添加了这一行

<uses-permission android:name="android.permission.INTERNET" /> 

并在</application>标记

之外写下

答案 1 :(得分:1)

public String getXmlElement(String url) {

    String xml=null;
    try{
    DefaultHttpClient client=new DefaultHttpClient();
    Log.d("cli", "ok");
    HttpPost post= new HttpPost(url);
    Log.d("post", "ok"+post);
    HttpResponse responce=client.execute(post);
    Log.d("responce", "ok");
    HttpEntity entity=responce.getEntity();
    Log.d("entity", "ok");
    xml=EntityUtils.toString(entity);
    }catch(UnsupportedEncodingException e){
        e.printStackTrace();
    }catch (IOException e) {
        // TODO: handle exception
        e.printStackTrace();
    }
catch(NullPointerException e){
   Toast.makeText(getApplicationContext(), "Connection to server failed.. Please try later..", Toast.LENGTH_LONG).show();
}
    return xml;

}

您可以使用上面显示的抓取向用户显示他可能未连接到互联网。空指针异常可能出现在任何一行,因此它不准确。

或者您可以将 if else条件与响应放在一起:

 if(responce!=null){
   HttpEntity entity=responce.getEntity();
  }
else{
     Toast.makeText(getApplicationContext(), "Connection to server failed.. Please try later..", Toast.LENGTH_LONG).show();
}

未提及互联网许可

答案 2 :(得分:0)

您正在尝试直接使用Asyntask响应中的空字符串。因此获得NPE。

还要在清单中给予INTERNET权限以进行网络相关操作。