sisko
sisko

Reputation: 9910

Android AsyncTask-to-Activity callback nullPointerException

I am trying to pass some JSONObject data back from my AsyncTask.OnPostExecute to my MainActivity.JSONCallBackComplete.

My problem is when I attempt to check my passed-back JSONObject I get the following execption:

10-14 18:40:25.820: E/AndroidRuntime(4153): FATAL EXCEPTION: main
10-14 18:40:25.820: E/AndroidRuntime(4153): java.lang.NullPointerException
10-14 18:40:25.820: E/AndroidRuntime(4153):     at com.icerge.revivaltimes.MainActivity.JSONCallBackComplete(MainActivity.java:31)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at com.icerge.revivaltimes.JsonObj.onPostExecute(JsonObj.java:70)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at com.icerge.revivaltimes.JsonObj.onPostExecute(JsonObj.java:1)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at android.os.AsyncTask.finish(AsyncTask.java:417)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at android.os.AsyncTask.access$300(AsyncTask.java:127)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:429)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at android.os.Handler.dispatchMessage(Handler.java:99)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at android.os.Looper.loop(Looper.java:144)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at android.app.ActivityThread.main(ActivityThread.java:4937)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at java.lang.reflect.Method.invokeNative(Native Method)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at java.lang.reflect.Method.invoke(Method.java:521)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
10-14 18:40:25.820: E/AndroidRuntime(4153):     at dalvik.system.NativeStart.main(Native Method)

My MainActivity class is as follows:

public class MainActivity extends Activity{

        private JSONObject jsonData = null;

        public void JSONCallBackComplete(JSONObject jsonData){
            this.jsonData = jsonData;
            Log.e("TESTing in callback: ", jsonData.toString()  );
        }

        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);

            new JsonObj(this).execute("http://myserver/json");
         }
    }

And my AsyncTask class is as follows:

public class JsonObj extends AsyncTask<String, Void, JSONObject>{
    MainActivity activity;
    int tid;
    String term;

    public JsonObj(MainActivity activity){
        this.activity = activity;
//      Log.e("TESTING: ", activity.getClass().toString());
    }

    @Override
    protected JSONObject doInBackground(String... url) {
        // TODO Auto-generated method stub
        DefaultHttpClient   httpclient = new DefaultHttpClient(new BasicHttpParams());
        HttpPost httppost = new HttpPost(url[0]);
        JSONObject jsonObject = null;
        // Depends on your web service
        httppost.setHeader("Content-type", "application/json");

        InputStream inputStream = null;
        String result = null;
        try {
            HttpResponse response = httpclient.execute(httppost);           
            HttpEntity entity = response.getEntity();

            inputStream = entity.getContent();
            // json is UTF-8 by default
            BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream, "UTF-8"), 8);
            StringBuilder sb = new StringBuilder();

            String line = null;
            while ((line = reader.readLine()) != null){
                sb.append(line + "\n");
            }
            result = sb.toString();
            Log.e("JSON-Test [RESULT]: ", result);
            jsonObject = new JSONObject(result);
        } catch (Exception e) { 
            Log.e("JSON-Test [exception]: ", e.toString());
        }
        finally {
            try{if(inputStream != null)inputStream.close();}catch(Exception squish){}
        }

        return jsonObject;
    }

    @Override
protected void onPostExecute(JSONObject result) {
    // TODO Auto-generated method stub
    super.onPostExecute(result);
    if( !result.equals(null) ){
        this.activity.JSONCallBackComplete(result);
    }
}
}

I have a constructor in the AsyncTask assigning the MainActivity-Context and using it to call my JSONCallBackComplete feedback function with the JSONObject argument.

However, instead of displaying my JSON-DATA, I get the exception I pasted above.

Upvotes: 1

Views: 1323

Answers (1)

bclymer
bclymer

Reputation: 6771

You're having NullPointerExceptions. The first one I'm seeing is coming from here

if(!result.equals(null) ){

This should actually be

if (result != null) {

You shouldn't really use .equals() to compare to null, because .equals() is usually implemented to check if the human perceived values of 2 objects are the same, not memory address. When you want to compare something to the null memory address, just use the direct == or !=, where Java will compare the addresses for you (what you want).

Upvotes: 1

Related Questions