1

I'm sending some data from my app to an external server.

public class PostKey extends AsyncTask<String, String, String> {
    public AsyncResponse delegate = null;

    @Override
    protected String doInBackground(String... params) {
        postData(params[0]);
        return null;
    }

    @Override
    protected void onPostExecute(String result){
        Log.d(String.valueOf(result), " Result?");
        delegate.processFinish(result); // Returns NullPointerException
    }

    public void postData(String valueIWantToSend) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost     = new HttpPost("http://domain.com/post.php");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("hoi", "test"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());

            Log.d(responseStr, "");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
    }

    public interface AsyncResponse {
        void processFinish(String output);
    }
}

Log.d(responseStr, ""); does return true or false, depending on what data was send, and logs it.

delegate.processFinish(result); returns the NullPointerException also mentioning public class PostKey extends AsyncTask<String, String, String> eventhough all the parameters are declared as Strings.

I've also tried replacing result with responseStr but that didn't work either.

Orynuh
  • 105
  • 1
  • 10
  • Your activity implements the interface? – Raghunandan Jul 17 '15 at 11:51
  • @Raghunandan yes, it does – Orynuh Jul 17 '15 at 11:51
  • You return null from doInBackground method so you get null in onPostExecute – SANAT Jul 17 '15 at 11:52
  • You can pass the same to the constructor of asynctask and do `delegate= (AsyncResponse) activity;` Note : Activity implements the interface. – Raghunandan Jul 17 '15 at 11:53
  • `void postData` => `String postData`, `Log.d(responseStr, "");` => `return responseStr` (also add `return null;` at the end of this method), body of `doInBackground` => `return postData(params[0]);` ... add null check in `onPostExecute` ... still `delegate` can be null if you do not set it ... – Selvin Jul 17 '15 at 11:55
  • ... but still ... 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 Jul 17 '15 at 11:56
  • @sanatshukla there has to be a retun statement there, otherwise an error displays. With what could I replace it? Just `return;` isn't valid either – Orynuh Jul 17 '15 at 11:57
  • onPostExecute get the data return by doInBackground method. So, if you return it a null then you get null in onPostExecute. – SANAT Jul 17 '15 at 11:58
  • check my answer below – SANAT Jul 17 '15 at 12:04
  • you can check my answer below. – gmetax Jul 17 '15 at 12:09

3 Answers3

1

Problem with your method call delegate.processFinish(result); delegate is not initialized. So you are calling a method on null object.

Raghu Mudem
  • 6,793
  • 13
  • 48
  • 69
0

change your code to that

public class PostKey extends AsyncTask<String, String, String> {
    public AsyncResponse delegate = null;

    public PostKey(AsyncResponse asyncResponse) {
        delegate = asyncResponse;
    }   

    @Override 
    protected String doInBackground(String... params) {
        return postData(params[0]);
    } 

    @Override 
    protected void onPostExecute(String result){
        Log.d(String.valueOf(result), " Result?");
        delegate.processFinish(result);
    } 

    public String postData(String valueIWantToSend) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost     = new HttpPost("http://domain.com/post.php");

        try { 
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("hoi", "test"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());

            Log.d(responseStr, "");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block 
        } catch (IOException e) {
            // TODO Auto-generated catch block 
        } 
        return responseStr;
    } 

    public interface AsyncResponse { 
        void processFinish(String output);
    } 
} 
gmetax
  • 3,853
  • 2
  • 31
  • 45
0

If you want the result of postData() then change the return type of postData() from void to String and return that result to onPostExecute like this :

public class PostKey extends AsyncTask<String, String, String> {
    public AsyncResponse delegate = null;

    @Override
    protected String doInBackground(String... params) {
        return postData(params[0]);
    }

    @Override
    protected void onPostExecute(String result){
        Log.d(String.valueOf(result), " Result?");
        delegate.processFinish(result); // Returns NullPointerException
    }

    public String postData(String valueIWantToSend) {
        HttpClient httpclient = new DefaultHttpClient();
        HttpPost httppost     = new HttpPost("http://domain.com/post.php");

        try {
            List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
            nameValuePairs.add(new BasicNameValuePair("hoi", "test"));
            httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));

            HttpResponse response = httpclient.execute(httppost);
            String responseStr = EntityUtils.toString(response.getEntity());
            return responseStr;
            Log.d(responseStr, "");
        } catch (ClientProtocolException e) {
            // TODO Auto-generated catch block
        } catch (IOException e) {
            // TODO Auto-generated catch block
        }
       return null;
    }

    public interface AsyncResponse {
        void processFinish(String output);
    }
}
SANAT
  • 8,489
  • 55
  • 66