Android : Getting RuntimeException on AsyncTask

↘锁芯ラ 提交于 2019-12-11 08:09:48

问题


In the code I try to read from a web service with an http client. Then I want to write the Responce Phrase of the http responce to a text view. The UI is accessed through onPostExecute() so I don't know why I get this exception.

This is the code:

public class CustomersScreen extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.customersscreen);
}

public void LoginClicked(View view) {
    new Get_data().execute("http://localhost:51982/WcfDataService1.svc/Genre");
}

private class Get_data extends AsyncTask <String, Void, String> {

    @Override
    protected String doInBackground(String... url) {
        //InputStream instream = null;
        HttpClient httpclient = new DefaultHttpClient();
        HttpGet request = new HttpGet(url[0]);
        HttpResponse response = null;
        try {
            response = httpclient.execute(request);
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        String phrase = response.getStatusLine().getReasonPhrase();

        return phrase;
    }

    @Override
    protected void onPostExecute(String phrase) {
        TextView t = (TextView) findViewById(R.id.textView3);
        t.setText(phrase);
    }

}

}

and the log cat :

09-28 09:43:34.239: E/AndroidRuntime(679): FATAL EXCEPTION: AsyncTask #1
09-28 09:43:34.239: E/AndroidRuntime(679): java.lang.RuntimeException: An error occured while executing doInBackground()
09-28 09:43:34.239: E/AndroidRuntime(679):  at android.os.AsyncTask$3.done(AsyncTask.java:278)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.util.concurrent.FutureTask.run(FutureTask.java:137)
09-28 09:43:34.239: E/AndroidRuntime(679):  at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:208)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.lang.Thread.run(Thread.java:856)
09-28 09:43:34.239: E/AndroidRuntime(679): Caused by: java.lang.NullPointerException
09-28 09:43:34.239: E/AndroidRuntime(679):  at com.test.CustomersScreen$Get_data.doInBackground(CustomersScreen.java:46)
09-28 09:43:34.239: E/AndroidRuntime(679):  at com.test.CustomersScreen$Get_data.doInBackground(CustomersScreen.java:1)
09-28 09:43:34.239: E/AndroidRuntime(679):  at android.os.AsyncTask$2.call(AsyncTask.java:264)
09-28 09:43:34.239: E/AndroidRuntime(679):  at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
09-28 09:43:34.239: E/AndroidRuntime(679):  ... 5 more

回答1:


Caused by: java.lang.NullPointerException
09-28 09:43:34.239: E/AndroidRuntime(679):  at com.test.CustomersScreen$Get_data.doInBackground(CustomersScreen.java:46)

You have a NPE in line 46 of your class. The code, posted lacks some lines obviously, but I suppose it's this line String phrase = response.getStatusLine().getReasonPhrase(); as response would be null if anything goes wrong with the httpclient.execute(request);

So, just put that line within the try/catch block as well and add a simple return null; at the end of the method instead.



来源:https://stackoverflow.com/questions/12640470/android-getting-runtimeexception-on-asynctask

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!