Raphael Pineda
Raphael Pineda

Reputation: 420

Android retrieving data from webservice asynctask nullpointer

Here is my code:

http://pastie.org/4856109

What I want to achieve is that I want to get all the data retrieved from my inner AsyncTask to be placed on my main_activity variable named nodes...

List nodes is the variable in which i want to place all the retrieved data... any suggestion on how i could store the data retrieved to be used in the class.

if i run this code the LogCat would give me this:

09-28 23:00:24.978: E/Downloads(1200): java.lang.NullPointerException

09-28 23:00:25.158: D/gralloc_goldfish(1200): Emulator without GPU emulation detected.

09-28 23:00:25.608: D/JSONClient(1200): "http://10.0.2.2/thesis/displayV.php"

Upvotes: 0

Views: 192

Answers (1)

dave.c
dave.c

Reputation: 10908

When you call:

readWebpage(null);

this makes an asynchronous call to populate your nodes object. However your main thread will immediately continue to this line:

textView.setText((nodes.isEmpty())?"true":"false");

Since nodes is still null at this point (as your AsyncTask has not yet completed) then you will get the NullPointerException that you are experiencing.

As @njzk2 states, you should move your UI manipulation into onPostExecute():

@Override
protected void onPostExecute(String result) {
    textView.setText((nodes.isEmpty())?"true":"false");
}

Upvotes: 1

Related Questions