0

I'm trying to show a list but the app stop, I'm new with web service and Asynctask. I think.. I have a problem with ArrayList> contactListDelete I knew I had to investigate initializing but did not know where now that initializes the ArrayList returned me this error... this is LogCat (ERROR):

09-12 00:13:51.745 4047-4047/com.injob.injob.injobapp W/dalvikvm: threadid=1: thread exiting with uncaught exception (group=0x4207bda0)
09-12 00:13:51.755 4047-4047/com.injob.injob.injobapp E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.injob.injob.injobapp, PID: 4047
java.lang.NullPointerException
at com.injob.injob.injobapp.Eliminar$DeleteContacts.onPostExecute(Eliminar.java:120)
at com.injob.injob.injobapp.Eliminar$DeleteContacts.onPostExecute(Eliminar.java:42)
at android.os.AsyncTask.finish(AsyncTask.java:632)
at android.os.AsyncTask.access$600(AsyncTask.java:177)
at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:645)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5635)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1291)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1107)
at dalvik.system.NativeStart.main(Native Method)

MY CODE:

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.HashMap;

public class Eliminar extends AppCompatActivity {

    private String TAG = Listar.class.getSimpleName();
    private ProgressDialog pDialogDelete;
    private ListView lvDelete;
    // URL to get contacts JSON
    private static String urlDel = "http://drwaltergarcia.com/InjobApp/getEmpleados.php";
    ArrayList<HashMap<String, String>> contactListDelete= null;


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

        lvDelete = (ListView) findViewById(R.id.list);

        new DeleteContacts().execute(); //llama al hilo que realizara la coneccion y extraccion de la data
    }

    /**
     * Async task class to get json by making HTTP call
     */
    private class DeleteContacts extends AsyncTask<Void, Void, Void> {


        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialogDelete = new ProgressDialog(Eliminar.this);
            pDialogDelete.setMessage("Please wait...");
            pDialogDelete.setCancelable(false);
            pDialogDelete.show();

        }

        @Override
        protected Void doInBackground(Void... arg0) {
            ArrayList<HashMap<String, String>> contactListDelete = new ArrayList<HashMap<String, String>>();
            HttpHandler shi = new HttpHandler();
            // Making a request to url and getting response
            String jsonStr = shi.makeServiceCall(urlDel);

            if (jsonStr != null) {
                try {
                    JSONArray contacts = new JSONArray((jsonStr)); //como sabemos que lo primero que llega es un array usamos esto caso contrario un JsonObject
                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {

                        JSONObject c = contacts.getJSONObject(i); //dentro del array vienen objetos se los toma de esta manera

                        // se crean variables que vayan guardando los datos para su posterior uso

                        String id = c.getString("id");
                        String nombre = c.getString("nombre");
                        String apellido = c.getString("apellido");
                        String email = c.getString("email");
                        String password = c.getString("password");
                        String cedula = c.getString("cedula");
                        String sueldo = c.getString("sueldo");
                        String horaEntrada = c.getString("horaEntrada");
                        String horaSalida = c.getString("horaSalida");
                        String Multa = c.getString("Multa");

                        // tmp hash map for single contact
                        HashMap<String, String> contact = new HashMap<String, String>();

                        // adding each child node to HashMap key => value
                        contact.put("id", id);
                        contact.put("nombre", nombre);
                        contact.put("apellido", apellido);
                        contact.put("Multa", Multa);

                        // adding contact to contact list
                        contactListDelete.add(contact);
                    }
                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                }
            } else {
                Log.e(TAG, "Couldn't get json from server.");
            }
            return null;
        }


        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            // Dismiss the progress dialog
            if (pDialogDelete.isShowing())
                pDialogDelete.dismiss();
            /**
             * Updating parsed JSON data into ListView
             * */
            //se crea un adaptador del listview que es como se mostrara los datos recibidos en este caso es list_item. se le envia los datos y en donde se los ubicara
            ListAdapter adapter = new SimpleAdapter(
                    Eliminar.this, contactListDelete,
                    R.layout.list_item, new String[]{"id","nombre", "apellido", "Multa"},
                    new int[]{R.id.id, R.id.name, R.id.apellido, R.id.multa});
            lvDelete.setAdapter(adapter);
        }

    }
}
Yasin Kaçmaz
  • 5,867
  • 4
  • 34
  • 54
Justhine
  • 1
  • 1
  • 1
    Also see, [What is a stacktrace](http://stackoverflow.com/questions/3988788/what-is-a-stack-trace-and-how-can-i-use-it-to-debug-my-application-errors) – codeMagic Sep 12 '16 at 22:57
  • You have two separate instances of `contactDeleteList`. The one in doInBackground is not assigning the other one, but instead making a brand new variable of the same name – OneCricketeer Sep 12 '16 at 23:29
  • even if you remove an equal I get the error – Justhine Sep 13 '16 at 02:53

0 Answers0