我的代码出了什么问题? (java.lang.NullPointerException)

时间:2013-04-28 06:10:49

标签: java android xml http

我有这个功能:

public static String getXML(int id, int choose, int foo){    
        String line = null;
        try {
            StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder().permitAll().build();
            StrictMode.setThreadPolicy(policy);
            DefaultHttpClient httpClient = new DefaultHttpClient();
            HttpPost httpPost = null;
            if(id == 100)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/logFollow.php?id="+ foo + "");
            }
            else if(id == 20)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/logMyTimeLine.php?id="+ foo + "");
            }
            else if(id == 19)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/logMyEntity.php?id="+ foo + "");
            }
            else if(choose == 1)
            {
                httpPost = new HttpPost("http://andronekat.com/sawalef/log.php?id="+ id + "");
            }
            else if(choose == 2)
                httpPost = new HttpPost("http://andronekat.com/sawalef/log2.php?id="+ id + "");
            else if(choose == 3)
                httpPost = new HttpPost("http://andronekat.com/sawalef/comments.php?tag="+ id + "");
            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            line = EntityUtils.toString(httpEntity, "UTF-8");

        }

除了第一个ULR之外,所有String URL都可以正常工作:

    if(id == 100)
    {
        httpPost = new HttpPost("http://andronekat.com/sawalef/logFollow.php?id="+ foo + "");
    }

它在这行中给了我java.lang.NullPointerException

HttpResponse httpResponse = httpClient.execute(httpPost);

我不知道从哪里得到null。如果我把它放在浏览器中,链接工作正常。有人可以帮帮我吗?

logcat的:

04-28 09:37:34.500: E/AndroidRuntime(21842): FATAL EXCEPTION: main
04-28 09:37:34.500: E/AndroidRuntime(21842): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.softshell.sowalef/com.softshell.sowalef.Follow}: java.lang.NullPointerException
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2100)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.os.Handler.dispatchMessage(Handler.java:99)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.os.Looper.loop(Looper.java:137)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.main(ActivityThread.java:4898)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at java.lang.reflect.Method.invokeNative(Native Method)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at java.lang.reflect.Method.invoke(Method.java:511)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at dalvik.system.NativeStart.main(Native Method)
04-28 09:37:34.500: E/AndroidRuntime(21842): Caused by: java.lang.NullPointerException
04-28 09:37:34.500: E/AndroidRuntime(21842):    at com.softshell.sowalef.Follow.onCreate(Follow.java:71)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.Activity.performCreate(Activity.java:5206)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1094)
04-28 09:37:34.500: E/AndroidRuntime(21842):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2064)
04-28 09:37:34.500: E/AndroidRuntime(21842):    ... 11 more

1 个答案:

答案 0 :(得分:4)

你是某些在失败的情况下id被设置为100吗?

即使您,您的代码中也有一个应该修复的漏洞。具体而言,id既不是10020也不是19choose的情况既不是1,也不是2,也不是3 {1}}。

而且,如果这不是您的特定问题(我保证它仍应修复),您还应检查httpPosthttpClient之前的值execute致电,以便您可以更轻松地找出问题所在。