0

I'm calling an Async class from my main activity class. When the POST has been executed I want to return the result back to the main activity.

public class MainActivity extends Activity implements OnClickListener, AsyncResponse{
    public Context context;
    PostKey asyncTask = new PostKey(context);

    public void onCreate(Bundle savedInstanceState) {
        asyncTask.delegate = this;
    }

    public void onClick(View v) {
        asyncTask.delegate = this;
        new PostKey(context).execute(keyValue);
    }

    public void processFinish(String output){
        //this you will received result fired from async class of    onPostExecute(result) method.
        Log.d("Result", output);
    }
}


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

    public PostKey(Context context){
        this.context = context.getApplicationContext();
    }

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

    @Override
    protected void onPostExecute(String result){
        this.context = context.getApplicationContext();
        delegate = (AsyncResponse) context;
    }

    delegate.processFinish(result);
}

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

Whenever I try to run the app I immediately get a fatal error caused by a nullpointer exception. The nullpointer refers to the following:

public PostKey(Context context){
    this.context = context.getApplicationContext();
}

& PostKey asyncTask = new PostKey(context);

In the second case I can get that context is empty, but I have to pass the variable here.

Orynuh
  • 105
  • 1
  • 10
  • In your main activity, `public Context context;` is declared but never given a value? It is null.. so trying to call the PostKey constructor, which attempts to invoke the `getApplicationContext()` function on a null object will definitely give you a NullPointerException. – almightyGOSU Jul 20 '15 at 08:13
  • 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) – almightyGOSU Jul 20 '15 at 08:15
  • @Gosu In every code snippet I found the `context` variable was set in the async class. Because of `private PostKey(Context context){}` I have to pass the variables in the main activity – Orynuh Jul 20 '15 at 08:18

3 Answers3

2

Activity is already a Context, so you don't to keep a reference to it. Just use this. On the other hand the Activity has to go through its lifecycle before you can use the context. Remove

public Context context;
PostKey asyncTask = new PostKey(context);

and add

PostKey asyncTask = new PostKey(this);

in your onCreate. And please, add super.onCreate(savedInstanceState); as first thing in your onCreate

Blackbelt
  • 156,034
  • 29
  • 297
  • 305
0

Hello Oryna you passing null context value as in parameter just replace these lines with this

 new PostKey(context).execute(keyValue);

to

 new PostKey(MainActivity.this).execute(keyValue);

and the constructor for the async task replace the code

    public PostKey(Context context){
    this.context = context.getApplicationContext();
}

with

public PostKey(Context context){
    this.context = context;
}
luckwithu
  • 143
  • 4
0

try to use this.context = getApplicationContext(); instead

karimkhan
  • 321
  • 3
  • 18