JaviCaselles
JaviCaselles

Reputation: 23

NullPointer when using asyncTask

Well I'm starting to program in Android and I'm doing an app that connects to a PHP file in a server and then reads the data in JSON. The app is almost working. It reads the JSON data from the file and I can insert text with the JSON value in the "onPostExecute" handler.

protected void onPostExecute(String string) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                txt1.setText(jo.toString() + "  " + jo.length());
                try {
                    //Creating an array from the JSON Object
                    jarray = jo.getJSONArray("Anunciante");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }


It works, but after that first execution, I cant use the JSONObject or the JSONArray because they are always null.

    try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

I have no clue what the problem is.

public class Main extends Activity {
public ProgressDialog pDialog;
JSONParser jParser = new JSONParser();
public static String urlPoblaciones = "http://192.168.1.168/Prueba/multitabla.php";
TextView txt1;
public static JSONObject jo;
public static JSONArray jarray;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    txt1 = (TextView) findViewById(R.id.txt1);

    new CargarPoblaciones().execute();

    try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}

class CargarPoblaciones extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        pDialog = new ProgressDialog(Main.this);
        pDialog.setMessage("Cargando poblaciones");
        pDialog.setIndeterminate(false);
        pDialog.setCancelable(false);
        pDialog.show();
        Log.d("Cargando: ", "Aun se esta cargando");
    }

    @Override
    protected String doInBackground(String... params) {
        // TODO Auto-generated method stub
        Log.d("Cargando: ", "Vamos a cargar los productos");
        jo = jParser.makeHttpRequest(urlPoblaciones);
        Log.d("JSON", jo.toString());
        return "Hola";
    }

    @Override
    protected void onPostExecute(String string) {
        pDialog.dismiss();
        runOnUiThread(new Runnable() {
            public void run() {
                /**
                 * Updating parsed JSON data into ListView
                 * */

                txt1.setText(jo.toString() + "  " + jo.length());
                try {
                    //Creating an array from the JSON Object
                    jarray = jo.getJSONArray("Anunciante");
                } catch (JSONException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        });
    }
}

}

Upvotes: 2

Views: 75

Answers (1)

Pararth
Pararth

Reputation: 8134

The array gets populated only after onPostExecute and the way you are calling it in onCreate, the response isn't parsed yet so you get null there.

shift your code:

  try {
        if (jarray.equals(null)) {

        } else {

        }
    } catch (Exception e) {
        Toast.makeText(this, "Null Value", Toast.LENGTH_LONG).show();
    }

to inside onPostExecute after :

 jarray = jo.getJSONArray("Anunciante");  

You already have a progress bar which you dismiss. Just call that dismiss after onPostExecute, or set a flag after the jarray is populated.

Upvotes: 2

Related Questions