-1

In my AsyncTask, I have doInBackground return an arrayof Movie objects. After searching on how to achieve that, I found this SO post and implemented the solution suggested by the marked answer. The only difference is that as oppose to running the AsyncTask in my onCreate method in the MainActivity, I have the AsyncTask running in the onOptionsItemSelected method in MainActivity.

I was not quite certain solution meant in his implementation of the processFinish method, so I implemented mine like so ( in MainActivity). The list_of_movies variable is a global variable that I intend to pass into a GridView later.

@Override
public void processFinish(Movie[] movies){
    for(int i = 0; i < movies.length; i++){
        list_of_movies[i] = movies[i];
    }
}

To check that I actually have all the movie objects within list_of_movies, I did a quick loop of Log after I execute the AsyncTask in my onOptionsItemsSelected

public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.action_popular) {
        FetchMovieTask movieTask = new FetchMovieTask();
        movieTask.delegate = this;
        movieTask.execute("popular");

        for(int i = 0; i < list_of_movies.length; i++){
            Log.v("Loop Logging", list_of_movies[i].getTitle());
        }

        return true;
    }

    return super.onOptionsItemSelected(item);
}

Which returns me a java.lang.NullPointerException

I've tried the following

1) I thought maybe I was not allow to call this operation within onOptionsItemSelected, so I tried the same operation in onCreate instead. Also the same error

2) I simply set list_of_movies = movies in the processFinish method, but that does not work either

What am I doing wrong here?

I thought maybe it's because I added the public to the processFinish method, but without public Android Studio would not let me compile due to some clashing issues. Another theory is that list_of_movies is pointing at nothing, but I thought that's the whole point of using interfaces to transfer the Movie[].

Sorry if I'm simply misunderstanding some fundamental concepts of Android, just started learning a few weeks ago. Thanks.

Community
  • 1
  • 1
user3277633
  • 1,891
  • 6
  • 28
  • 48

2 Answers2

3

I think you are misunderstanding what an AsyncTask is doing.

It is doing operations in the background, and when it is completed, goes to processFinish of the delegate interface. Meanwhile, all code after the execute method continues to run.

Therefore, would you be surprised if this caused an error? The list_of_movies variable contains no data yet.

for(int i = 0; i < list_of_movies.length; i++){
    Log.v("Loop Logging", list_of_movies[i].getTitle());
}

FetchMovieTask movieTask = new FetchMovieTask();
movieTask.delegate = this;
movieTask.execute("popular");
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
0

The list that you are returning from doInBackground is passed to the onPostExecute interface method. So you can do something like below inside you AsyncTask class.

@Override
protected void onPostExecute(Movie[] movies) {
    for(int i = 0; i < movies.length; i++){
    list_of_movies[i] = movies[i];
}

Here is another solution/answer that i gave which should give you a much better idea on how does Asynctask works in android.

Community
  • 1
  • 1
Gaurav Sarma
  • 2,248
  • 2
  • 24
  • 45