简体   繁体   中英

What's wrong with my code? ( java.lang.NullPointerException)

I have this function:

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");

        }

All the String URL works fine except for the first ULR which is:

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

it gives me java.lang.NullPointerException in this line

HttpResponse httpResponse = httpClient.execute(httpPost);

I don't know from where I got the null. The link is working fine if I put it in the browser. Can anyone help me please?

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

Are you certain that id is set to 100 in the failing case?

Even if you are, you have a hole in your code that should be fixed. Specifically, the case where id is neither 100 , 20 nor 19 and choose is neither 1 , 2 nor 3 .

And, if that's not your specific problem here (I warrant that it should still be fixed), you should also check your values for httpPost and httpClient before the execute call so you can more easily figure out where the problem lies.

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM