-1

I am making an application in which i wnat to show phone location on map I am succesful in sendin latitudes to server but when i am trying to get data from server i am getting and when i store it in hashmap then it shows null pointer exception my code is

package com.example.gpstracking;

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

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

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Supervisornew extends Activity {
    EditText et;
    Button bt;
    private ProgressDialog pDialog;

    // URL to get contacts JSON
    private static String url = "http://dyandroidapps.netii.net/android_db/dy.php";

    // JSON Node names
    private static final String TAG_CONTACTS = "products";
    private static final String TAG_LAT = "lat";
    private static final String TAG_LNG = "lng";

    // contacts JSONArray
    // JSONArray contacts = null;
    JSONArray contacts = null;
    // Hashmap for ListView
    ArrayList<HashMap<String, String>> contactList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.supervisor);
        et = (EditText) findViewById(R.id.et_supervisor);
        bt = (Button) findViewById(R.id.btn_supervisor);

    }

    public void track(View v) {
        // new LoadAllProducts().execute();
        new GetContacts().execute();
        // new RetrieveTask().execute();
    }

    private class GetContacts extends AsyncTask<Void, ArrayList<String>, Void> {

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

        @Override
        protected Void doInBackground(Void... arg0) {
            // Creating service handler class instance
            ServiceHandler sh = new ServiceHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url, ServiceHandler.GET);
            System.out.print(jsonStr);

            Log.d("Response: ", "> " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = new JSONObject(jsonStr);
                    contacts = jsonObj.getJSONArray(TAG_CONTACTS);

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        String lat = c.getString(TAG_LAT);
                        String lng = c.getString(TAG_LNG);
                        Log.d("response", lat);
                        Log.d("response", lng);//here i m getting values in log

                        HashMap<String, String> map=new HashMap<String,String>();
                        map.put("lat",lat);
                        map.put("lng", lng);
                        contactList.add(map);// i am getting nullpointer excetion



                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            } else {
                Log.e("ServiceHandler", "Couldn't get any data from the url");
            }


            return null;
        }

         @Override protected void onPostExecute(Void result) {

}
    }
}

and also tell me what do do in onpost execute to transfer arraylist to other activity Thanks in advance.........

DEEPAK YADAV
  • 21
  • 1
  • 9
  • http://stackoverflow.com/questions/23353173/unfortunately-myapp-has-stopped-how-can-i-solve-this – laalto Jul 12 '14 at 08:29

3 Answers3

1

In your code first initialize the contactList before using it,i.e. in onCreate() Method add this line

contactList = new ArrayList<HashMap<String, String>>();

Edit

And to transfer arraylist to other activity see this, How to pass ArrayList HashMap from one activity to another

Community
  • 1
  • 1
Giru Bhai
  • 14,370
  • 5
  • 46
  • 74
1

You forgot to create the object of contactList.You are accessing its reference which will result in NPE. you should create the object inside onCreate() as follows:

contactList = new ArrayList<HashMap<String, String>>();
kgandroid
  • 5,507
  • 5
  • 39
  • 69
0

The other two answers give you the reason for the Null Pointer so, answering the second part of your question i.e. passing ArrayList to another Activity :

Use putExtra(String, Serializable) to pass the value in an Intent and getSerializableExtra(String) method to retrieve the data.

Passing an ArrayList<HashMap<String, String>> from Activity A to Activity B

Intent intent = new Intent(this, B.class);
HashMap<String, String> hm = new HashMap<String, String>();
hm.put("key_name", "key_value");
ArrayList<HashMap<String, String>> arl = new ArrayList<HashMap<String, String>>();
arl.add(hm);
intent.putExtra("arraylist", arl);
startActivity(intent);

Retrieve the data in Activity B

ArrayList<HashMap<String, String>> arl = (ArrayList<HashMap<String, String>>) getIntent().getSerializableExtra("arraylist");

Reference : How to send hashmap value to another activity using an intent

Community
  • 1
  • 1
Shivam Verma
  • 7,973
  • 3
  • 26
  • 34