BufferReader buffer = new BufferReader() 给我一个 ERROR NullPointerException 并在 Debug 中停止

BufferReader buffer = new BufferReader() Gives me an ERROR NullPointerException and It stops there with Debug

我在 Eclipse 上尝试了 运行 这段代码,但这是我的一个错误。我正在尝试将 HTTP 发送到 php 站点并将响应作为 TextView 获取。

login.setOnClickListener(new View.OnClickListener(){

    @Override
    public void onClick(View v){
        try{
            test_email = email.getText().toString();

            String submit = "http://androidexample.com/media/webservice/httpget.php?user=" + test_email;

            URI websitetarget = new URI(submit);

            try{
                HttpClient Client = new DefaultHttpClient();

                HttpGet httpget = new HttpGet();
                httpget.setURI(websitetarget);
                HttpResponse response = Client.execute(httpget);

                // get the message from the response 
                HttpEntity entity = response.getEntity();

                if(entity == null){
                    content.setText("failed");  
                }

                InputStream webs = entity.getContent();
                InputStreamReader r = new InputStreamReader(webs);

                StringBuilder str = new StringBuilder();

                // convert response to string 
                try{ 
                    BufferedReader reader = new BufferedReader(r);

                    while((line = reader.readLine()) != null){
                        str.append(line + "\n");
                    }

                    str.toString();

                    content.setText(str);

                    webs.close();
                }catch(Exception e){
                    content.setText("failed");
                }
            }catch(Exception e){
                content.setText("FAIL");
            }
        }catch(Exception e){
            content.setText("FAIL");
        }
    }
});

我进入 Logcat

时出错
02-07 17:38:41.935: E/AndroidRuntime(28709): FATAL EXCEPTION: main
02-07 17:38:41.935: E/AndroidRuntime(28709): java.lang.NullPointerException
02-07 17:38:41.935: E/AndroidRuntime(28709):     at com.example.login_test.MainActivity.onClick(MainActivity.java:147)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at android.view.View.performClick(View.java:4240)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at android.view.View$PerformClick.run(View.java:17749)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at android.os.Handler.handleCallback(Handler.java:730)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at android.os.Looper.loop(Looper.java:137)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at android.app.ActivityThread.main(ActivityThread.java:5457)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at java.lang.reflect.Method.invokeNative(Native Method)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at java.lang.reflect.Method.invoke(Method.java:525)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:854)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:670)
02-07 17:38:41.935: E/AndroidRuntime(28709):     at dalvik.system.NativeStart.main(Native Method)

见我上面的评论。此外,如果 entity==null 在这里,您将得到 NullPointerException

            ...
            if(entity == null){
                content.setText("failed");  
            }

            InputStream webs = entity.getContent();
            ...

这应该是

            ...
            if(entity == null){
                content.setText("failed");  
                return;
            }

            InputStream webs = entity.getContent();
            ...