执行由NPE引起的doinbackground()时发生java.lang.runtimeexception错误

时间:2015-07-23 05:12:43

标签: java android json eclipse

每当我打开我的应用程序时,它崩溃并给我以下logcat错误。 我不知道我哪里出错了。我想知道为什么我的应用程序崩溃了,我该如何解决它。

??-?? ??:??:??.???: INFO/<unknown>(<unknown>): java.lang.RuntimeException: An error occured while executing doInBackground()
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at android.os.AsyncTask$3.done(AsyncTask.java:300)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at java.util.concurrent.FutureTask.run(FutureTask.java:242)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at java.lang.Thread.run(Thread.java:841)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>): Caused by: java.lang.NullPointerException
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at com.example.gravitas2015.MainActivity$jsonhandler.handleResponse(MainActivity.java:284)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at com.example.gravitas2015.MainActivity$jsonhandler.handleResponse(MainActivity.java:1)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:657)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:627)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:616)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at android.net.http.AndroidHttpClient.execute(AndroidHttpClient.java:273)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at com.example.gravitas2015.MainActivity$request.doInBackground(MainActivity.java:250)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at com.example.gravitas2015.MainActivity$request.doInBackground(MainActivity.java:1)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at android.os.AsyncTask$2.call(AsyncTask.java:288)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     at java.util.concurrent.FutureTask.run(FutureTask.java:237)
??-?? ??:??:??.???: INFO/<unknown>(<unknown>):     ... 4 more

这是我的doInBackground代码

private class request extends AsyncTask<Void, Void, List<String>>{


        AndroidHttpClient client= AndroidHttpClient.newInstance("");
        HttpGet request;

        @Override
        protected List<String> doInBackground(Void... arg0) {
            if (isOnline())
            {
                try{

                    request = new HttpGet("http://www.com");

                }catch(NullPointerException e ){
                    e.printStackTrace();}
                jsonhandler handler = new jsonhandler();
                try
                {
                    return client.execute(request,handler);
                }catch(ClientProtocolException exception){
                    exception.printStackTrace();
                }catch(IOException exception){
                    exception.printStackTrace();
                }

            }
            return null;
        }
        protected void onPostExecute(List<String> result)
          {
            super.onPostExecute(result);
            if (this.client != null) {
              this.client.close();
            }
          }

    }
private class jsonhandler implements ResponseHandler<List<String>>{
    @Override
    public List<String> handleResponse(HttpResponse response)
            throws ClientProtocolException, IOException {

        List<String> result = new ArrayList<String>();
        String jsonresponse = new BasicResponseHandler().handleResponse(response);
        int i;
        JSONArray data;
        try {

            data = new JSONArray(jsonresponse);
            for(i=0;i<data.length();i++)
            {
                JSONObject obj = (JSONObject)data.get(i);
                tid[i]=Integer.parseInt(obj.get("tid").toString());
                name[i]=obj.get("name").toString();
                org[i]=obj.get("organisation").toString();
                desc[i]=obj.getString("description").toString();
                date[i]=obj.get("date").toString();
                time[i]=obj.get("timings").toString();
                loc[i]=obj.get("location").toString();
                fee[i]=obj.get("fee").toString();
                cname1[i]=obj.get("cname1").toString();
                pho1[i]=obj.get("cno1").toString();
                cname2[i]=obj.get("cname2").toString();
                pho2[i]=obj.get("cno2").toString();
                email[i]=obj.get("email").toString();
                team[i]=obj.get("team").toString();


                result.add("EVENT: "+name[i]+"\nDATE: "+date[i]+"\nVENUE: "+loc[i]+"\n");
            }
            k=i;
        }catch (JSONException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        return result;
    }


}

1 个答案:

答案 0 :(得分:0)

在你的try区块中

request = new HttpGet("http://gravitas-eshwa.rhcloud.com/eventapi.php");

如果这导致catch NullPointerException失败并且

jsonhandler handler = new jsonhandler();

try
   {
       return client.execute(request,handler);
   }catch(ClientProtocolException exception){
         exception.printStackTrace();
   }catch(IOException exception){
       exception.printStackTrace();
   }

所以你只需进入

return client.execute(request,handler);

当您刚刚将request作为null时,会导致错误。因此,您必须向第一个catch添加内容以对null request做出反应。