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.