-2

First of all, my AsyncTask works perfect while fetching data to show on my activity. But when i click to start another activity, i get java.lang.NullPointerException error right here:

protected void onPostExecute(JSONObject jsonArray) {
        //super.onPostExecute(jsonArray);
        Error Here -> onTaskComplete.setMyTaskComplete(jsonArray);
        if(pDialog != null)
            pDialog.dismiss();
        /*if(jsonArray != null){
            arrVer(jsonArray);
        }*/
    }

Anybody has any idea what may cause the problem?

duggu
  • 37,851
  • 12
  • 116
  • 113
gokturk
  • 116
  • 2
  • 13
  • Maybe `onTaskComplete` or `jsonObject` are null? Have you checked it? – A.S. Mar 17 '15 at 11:02
  • 1
    what is onTaskComplete? check seems this is null for better suggestion post log – Dilip Mar 17 '15 at 11:03
  • java.lang.NullPointerException at com.example.deathstar.ankettest.Kategoriler.onPostExecute(Kategoriler.java:110) at com.example.deathstar.ankettest.Kategoriler.onPostExecute(Kategoriler.java:28) at android.os.AsyncTask.finish(AsyncTask.java:632) at android.os.AsyncTask.access$600(AsyncTask.java:177) at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645) – gokturk Mar 17 '15 at 11:05
  • And jsonObject is not null i checked it. Also the ontaskcomplete. ontaskcomplete is an interface of asynctask to pass data of it to asctivity. – gokturk Mar 17 '15 at 11:07
  • possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Selvin Mar 17 '15 at 11:44

1 Answers1

0

Try this...

protected void onPostExecute(JSONObject jsonArray) {
        //super.onPostExecute(jsonArray);

    if(jsonArray!=null){
        onTaskComplete.setMyTaskComplete(jsonArray);
        if(pDialog != null)
            pDialog.dismiss();
        //rest of your code
    }
}

PS:

Make sure that you are recieving JSONObject or JSONArray or String as paramter of this function. That could be reason for Exceptions.

Pragnesh Ghoda シ
  • 8,318
  • 3
  • 25
  • 40
  • It is obvious that `jsonArray` is not a problem here ... `jsonArray` would be a problem the error will appear in `setMyTaskComplete` of `onTaskComplete` implementation ... – Selvin Mar 17 '15 at 11:29
  • @Selvin : I have been through that condition not so much time ago.. Thats why I informed him to put check there for `NullPointerException` . If there is another problem that can be detected but for that we have to solve this. – Pragnesh Ghoda シ Mar 17 '15 at 11:32
  • stacktrace ends in `onPostExecute` ... even if `jsonArray` is `null` IT WILL NOT CAUSE NPE in `onPostExecute` ... you answer is good ... but not for this question (diplomatic way for telling you that this is not a valid answer) – Selvin Mar 17 '15 at 11:34
  • it will.. in some case when AsyncTask couldn't complete its execution properly. It will throw null response to `onPostExecute`. like in case of TIMEOUT, SERVER NOT FOUND etc. – Pragnesh Ghoda シ Mar 17 '15 at 11:36
  • no, it will not ... `jsonArray` is not a rval in `onPostExecute` ... so it doesn't matter if it is null or not ... it will not cause NPE in onPostExecute – Selvin Mar 17 '15 at 11:38
  • dude.. i had been through that case thats why i always check for NULL response in `onPostExecute`. You can't understand this condition until you pass through it.. So i can definitely say that AsynkTask can throw NULL response. – Pragnesh Ghoda シ Mar 17 '15 at 11:40
  • first, I'm not your dude ... second, it seems like you have a problems with basic code flow ... assertion should be done in setMyTaskComplete, becuase there jsonArray == null could be a problem ... it is obvious (such obvious that this question should be never asked) only one thing can casue NPE there, and is not a jsonArray == null – Selvin Mar 17 '15 at 11:41
  • 1
    OK, take it to that way.. if you have some better solution post your answer otherwise wait for the user who asked this question. If you can not help don't give suggestions to other people who are trying to help. – Pragnesh Ghoda シ Mar 17 '15 at 11:45
  • you are not trying to help, because you do not understand, what is going on there(in the code). He wrote *Error Here -> `onTaskComplete.setMyTaskComplete(jsonArray);`* NPE in this place vide: *java.lang.NullPointerException at com.example.deathstar.ankettest.Kategoriler.onPostExecute(Kategoriler.java:110)* can be only caused when `onTaskComplete` is null ... it is obvious, it is a simple logic, it is programming and `jsonArray` has nothing to do with this NPE – Selvin Mar 17 '15 at 15:34
  • if you do understand then post your answer. Otherwise don't suggest me what to do.. B'coz you are neither helping nor trying to help. You are just criticizing other people's work. I think that's the only thing you are doing here. – Pragnesh Ghoda シ Mar 18 '15 at 09:21