1

I've been researching all day trying to find out how to retrieve the values computed in the doInBackground async task. No luck at all.

I'm doing a basic HttpURLConnection request and parsing some XML data from a webpage using the DOM. I successfully store the data in two different arrays

///////////inside doInBackground:
for(int x=0; x<10; x++)
{
    username[x] = element.getFirstChild().getNodeValue();
    score[x] = anotherElement.getFirstChild().getNodeValue();
}

Now, all I want to do is simply output the values onto a textView.

Among many other things, I have attempted:

protected void onPostExecute(String result) 
{
        for (int xx = 0; xx<10; xx++)
        {
            theMainTextView.append(username[xx] + " scored " + score[xx] +"\n");
        }               
}

Nothing I have attempted works. A recurring error I'm receiving is the NullPointerException. Am I doing something dramatically incorrect? Know of any other (even obscure) methods I could try? Ignore the for loops if that helps...I've omitted a lot of code. Just assume I want to retrieve two values...a username and a score.

Edit: I should probably mention that the AsyncTask ends with return null;

Edit: apparently the code is not faulty but I had a globally declared button which was causing a null Pointer Exception. Sorry about that.

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
xa.
  • 335
  • 2
  • 5
  • 13

3 Answers3

0

If you get a NullPointerException as stated in the question and this is all of your onPostExecute() code than the field theMainTextView must be null.

You must initialize it before starting the AsyncTask - best place to do so is in onCreate() for Activities or onCreateView() for Fragments.

Wolfram Rittmeyer
  • 2,402
  • 1
  • 18
  • 21
  • Oh gosh, I declared a button globally. I don't ever use the button but I had `Button button =(Button) findViewById(R.id.button)` in the global class. – xa. May 21 '13 at 08:13
0

Although it's not the best practice, your code should work. I think the problem comes from another part. Can you please specify what line is throwing the NullPointerException?

Corbella
  • 1,791
  • 14
  • 24
  • Yeh I know. I'm just trying to learn all different ways of doing things. The issue was indeed from another part of the code. – xa. May 21 '13 at 08:14
0

To retrieve values from an AsyncTask you can use listener.

First create interface listner (new file):

public interface AsyncListener {

        void onAsyncFinishMethod(String params);
}

Second, use implement for your main class where you call async task (example)

public class MainActivity implements AsyncListener {

Third, create full body for listener method in your main class. You are overriding method from interface. So if you change params you will have to change too in interface. Here you will get all results after task finish and call onPostExecute.

@Override
public void onAsyncFinishMethod(String params) {
    Log.d("xxx", params);
}

Fourth, set listener for your async task. It means: In your async task class create this method

public void setOnAsyncFinishedMethod(AsyncListener listener) {
    this.listener = listener;
}

Make sure, your async task has private param with type that listener

private AsyncListener listener;

In onPostExecute in async task class call listener method as a last (if you don't have this method, please create it)

@Override
protected void onPostExecute(String params) {
    listener.onAsyncFinishMethod(param);
}

Last step, during calling async task in your main class don't forget bind setOnAsyncFinishedMethod method to it

My Example:

private void runMyAsyncTask() {
    CustomAsync async = new CustomAsync();
    async.setOnAsyncFinishedMethod(this);//<<< before execute use setOnAsyncFinishedMethod
    thread.execute();
}

Of course, params used in onAsyncFinishedMethod could be different than you, also onPostExecute.

deadfish
  • 11,996
  • 12
  • 87
  • 136
  • I had already looked into and attempted a similar method, however found it unnecessarily complex for the simple task I needed to achieve. Thanks for the answer. – xa. May 21 '13 at 08:25