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.........