0

Whenever I open my app, it crashes and gives me the following logcat errors. I do not know where I have gone wrong. I wanted to know why my application crashes and how can I fix it.

??-?? ??:??:??.???: 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

Here's my code for 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;
    }


}
  • https://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Karakuri Jul 23 '15 at 05:14
  • I did. It was the first fetch statement tid[i]=Integer.parseInt(obj.get("tid").toString()); – Steve Mathews Jul 23 '15 at 05:18
  • @Karakuri could you tell me how to avoid this specific NPE issue? – Steve Mathews Jul 23 '15 at 05:20
  • @SteveMathews Something on the line specified is `null`, you need to figure out what. It could either be `tid`, `obj`, or the result of `obj.get("tid")`. – Karakuri Jul 23 '15 at 05:27
  • i think `response` value is `null` as your `http://www.com` is not a valid address to get JSON data. – Anand Singh Jul 23 '15 at 05:29
  • That's just for confidentiality. There is an actual link. :D – Steve Mathews Jul 23 '15 at 05:39
  • Do null check of your response and print what is coming in your data . check the length of your json array. Do null checks where ever you are getting your data. Debug the code and see where the control is going and you will find exactly where it is failing. You can start using google's own volley library's GsonRequest class and you will be free from the entire hassle of parsing the response etc. GsonRequest will automatically set the setter methods of your result model class – Kaveesh Kanwal Jul 23 '15 at 06:06

1 Answers1

0

in your try block you say

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

if this fails you catch NullPointerException and do

jsonhandler handler = new jsonhandler();

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

So you simply proceed to

return client.execute(request,handler);

when you have just caught request as null, which causes the error. So you have to add something to the first catch to react to the null request.

milez
  • 2,201
  • 12
  • 31