modabeckham
modabeckham

Reputation: 247

Nullpoint exception even after getting data from json response

I'm trying to get data by calling webservices, for that I'm using a Async task class. As I have shon in the screen shot I'm getting json response successfully. But it crashes in listener.onTaskCompletedObject(responseJson); inside the onPostExecute(). Can anyone spot the reason wht this is not working for me.

Activity.java

       new AddressAsyncTask(getBaseContext(), new OnTaskCompletedObject() {

            @Override
            public void onTaskCompletedObject(JSONObject responseJson) {

                Constants.dataAddress = responseJson.toString();

                loaddata();

            }
        }).execute(email);

OnTaskCompletedObject.java

public interface OnTaskCompletedObject {
    void onTaskCompletedObject(JSONObject responseJson);
}

AddressAsyncTask.java

public class AddressAsyncTask extends AsyncTask<String, Integer, JSONObject> {

    private OnTaskCompletedObject listener;
    private JSONObject responseJson = null;
    private Context contxt;
    private Activity activity;
    String email;

    public AddressAsyncTask(Context context, OnTaskCompletedObject onTaskCompletedObject) {

        this.contxt = context;
    }

    // async task to accept string array from context array
    @Override
    protected JSONObject doInBackground(String... params) {

        String path = null;
        String response = null;
        HashMap<String, String> request = null;
        JSONObject requestJson = null;
        DefaultHttpClient httpClient = null;
        HttpPost httpPost = null;
        StringEntity requestString = null;
        ResponseHandler<String> responseHandler = null;

        // get the email and password
        Log.i("Email", params[0]);

        try {

            path = "http://xxxxxxxxxxxxxxxxx/MemberDetails";
            new URL(path);
        } catch (MalformedURLException e) {

            e.printStackTrace();
        }

        try {

            // set the API request
            request = new HashMap<String, String>();
            request.put(new String("Email"), params[0]);
            request.entrySet().iterator();

            // Store locations in JSON
            requestJson = new JSONObject(request);
            httpClient = new DefaultHttpClient();
            httpPost = new HttpPost(path);
            requestString = new StringEntity(requestJson.toString());

            // sets the post request as the resulting string
            httpPost.setEntity(requestString);
            httpPost.setHeader("Content-type", "application/json");

            // Handles the response
            responseHandler = new BasicResponseHandler();
            response = httpClient.execute(httpPost, responseHandler);

            responseJson = new JSONObject(response);

        } catch (Exception e) {
            Log.e("Buffer Error", "Error converting result " + e.toString());
        }
        try {
            responseJson = new JSONObject(response);

        } catch (JSONException e) {
            Log.e("JSON Parser", "Error parsing data " + e.toString());
        }

        return responseJson;

    }

    @Override
    protected void onPostExecute(JSONObject result) {
        // TODO Auto-generated method stub
        super.onPostExecute(result);

        listener.onTaskCompletedObject(responseJson);
    }

}

Screenshot of the exception

enter image description here

Screenshot of the jsonresponse

enter image description here

Upvotes: 0

Views: 125

Answers (4)

surhidamatya
surhidamatya

Reputation: 2609

You have forgotten to initialize your listener in AddressAsyncTask constructor.

public AddressAsyncTask(Context context, OnTaskCompletedObject onTaskCompletedObject) {

    this.contxt = context;
    this.listener=onTaskCompletedObject;
}

Upvotes: 0

Jagdeep Singh
Jagdeep Singh

Reputation: 1210

You have forgotten to initialize your listener in the asyn task constructor.

Upvotes: 0

Pooja Gaonkar
Pooja Gaonkar

Reputation: 1566

Try changing

listener.onTaskCompletedObject(responseJson);

to

listener.onTaskCompletedObject(result);

Upvotes: 1

ρяσѕρєя K
ρяσѕρєя K

Reputation: 132972

NullPointerException exception even after getting data from json response

Because listener is null.

Initialize listener as :

public AddressAsyncTask(Context context, OnTaskCompletedObject listener) {    
        this.contxt = context;
        this.listener=objListener;
    }

Upvotes: 3

Related Questions