1

i get this error in fetching json(null values):

03-21 12:32:02.918: E/AndroidRuntime(20383): FATAL EXCEPTION: AsyncTask #2
03-21 12:32:02.918: E/AndroidRuntime(20383): java.lang.RuntimeException: An error occured while executing doInBackground()
03-21 12:32:02.918: E/AndroidRuntime(20383):    at android.os.AsyncTask$3.done(AsyncTask.java:299)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask.run(FutureTask.java:137)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.lang.Thread.run(Thread.java:856)
03-21 12:32:02.918: E/AndroidRuntime(20383): Caused by: java.lang.NullPointerException
03-21 12:32:02.918: E/AndroidRuntime(20383):    at Dic.proj.pkg.notifService$check_last_asyn.doInBackground(notifService.java:253)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at Dic.proj.pkg.notifService$check_last_asyn.doInBackground(notifService.java:1)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at android.os.AsyncTask$2.call(AsyncTask.java:287)
03-21 12:32:02.918: E/AndroidRuntime(20383):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305)
03-21 12:32:02.918: E/AndroidRuntime(20383):    ... 4 more

and this is my check_last_asyn AsynTask:

// check if notification is available asyntask
class check_last_asyn extends AsyncTask<Void, Integer, Boolean> {
    @Override
    protected Boolean doInBackground(Void... arg0) {

        if(isOnline()){
        RestClient client = new RestClient(
                "http://mysite.org/api/check_last.php");
        client.AddParam("last", String.valueOf(last_notif_no));
        try {
            client.Execute(RequestMethod.GET);
        } catch (Exception e) {
            e.printStackTrace();
        }
        String response = client.getResponse();
        if(!response.equals(null)){
            update_response = response;
        }else{
            update_response= "{'update':'no'}";
        }
        }
        return true;
    }
}

i know this is because of null values but i dont know how to controll null values in this code...

Fcoder
  • 9,066
  • 17
  • 63
  • 100

4 Answers4

1

the check should be something like:

if(response != null && !response.equals(""))
Nermeen
  • 15,883
  • 5
  • 59
  • 72
1

This should do

if(response!=null && !(response.trim()).equals("")){
    update_response = response;
}else{
    update_response= "{'update':'no'}";
}
Jay Mayu
  • 17,023
  • 32
  • 114
  • 148
1

I think, String responce var could be null, so just add check for null value:

String responce = client.getResponce();

if (responce == null) {
    // As flag, which represents, that there is no answer from service
    // Or other logic
    return false; 
}

// your code
Artem Zinnatullin
  • 4,305
  • 1
  • 29
  • 43
1

you may control like

if(!TextUtils.isEmpty(response)){
        try {
                JSONObject  jobj =  new JSONObject(response);               
                String stolan_id = null;
                Boolean bstolan_id = jobj.has("keyhere");
                if(bstolan_id){             
                    stolan_id = jobj.getString("keyhere"); 
                }
       } catch (JSONException e) {
                e.printStackTrace();
            }

    }
Ankitkumar Makwana
  • 3,475
  • 3
  • 19
  • 45