I am trying to fill a global ArrayList
of JSONObjects
in an AsycTask
, but my app keeps crashing and giving me a null pointer exception. I am setting the arraylist = the ArrayList
that the "doInBackground
" returns to "onPostExecute
", but it is not actually populating my arrayList.
Am I missing something?
private class checkPlacesAround extends AsyncTask
> {
protected ArrayListdoInBackground(LatLng... ll) {
ArrayListar = getPlacesArray(ll[0]);
return ar;
}
protected void onProgressUpdate(Integer... progress) {
}
protected void onPostExecute(ArrayListresult) {
placesJSONArray = result;
}
/**
* give this a LatLng object and it will
* get the return an ArrayList of JSONObjects
* which includes that are the distances of
* all the places in the database from the
* LatLng point.
*
* @param ll LatLng
* @return ArrayList
*/
private ArrayListgetPlacesArray(LatLng ll) {
ArrayListjsonArray = new ArrayList ();
HttpURLConnection httpconn;
Cursor c = getPlaces();
c.moveToFirst();
while(!c.isLast()){
String type = c.getString(0);
String stringUrl = "https://maps.googleapis.com/maps/api/place/nearbysearch/" +
"json?location="+ ll.latitude +","+ ll.longitude +
"&radius=500&" +
type + "&" +
"sensor=false&" +
"key=" + apiKey;
URL url;
try {
url = new URL(stringUrl);
StringBuilder response = new StringBuilder();
try {
httpconn = (HttpURLConnection)url.openConnection();
if(httpconn.getResponseCode() == HttpURLConnection.HTTP_OK){
try {
BufferedReader input = new BufferedReader(new InputStreamReader(httpconn.getInputStream()));
String strLine = null;
while((strLine = input.readLine()) != null){
response.append(strLine);
}
input.close();
}catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try {
JSONObject json = new JSONObject(response.toString());
jsonArray.add(json);
Log.e("getting json", jsonArray.get(0).toString());
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
c.moveToNext();
}
return jsonArray;
}
/**
* returns a Cursor that contains the places
* @return
*/
private Cursor getPlaces(){
SQLiteDatabase db = openOrCreateDatabase("MyDB", MODE_PRIVATE, null);
try{
Cursor c = db.rawQuery("SELECT name FROM placesToGoArray", null);
c.moveToFirst();
return c;
}
catch(android.database.sqlite.SQLiteException e){
return null;
}
}
}
here is my error from logcat
04-27 01:14:12.786: E/AndroidRuntime(25662): FATAL EXCEPTION: main
04-27 01:14:12.786: E/AndroidRuntime(25662): java.lang.NullPointerException
04-27 01:14:12.786: E/AndroidRuntime(25662): at winlab.findplaces2.JSONstuff$getJSONDirections.onPostExecute(JSONstuff.java:226)
04-27 01:14:12.786: E/AndroidRuntime(25662): at winlab.findplaces2.JSONstuff$getJSONDirections.onPostExecute(JSONstuff.java:1)
04-27 01:14:12.786: E/AndroidRuntime(25662): at android.os.AsyncTask.finish(AsyncTask.java:631)
04-27 01:14:12.786: E/AndroidRuntime(25662): at android.os.AsyncTask.access$600(AsyncTask.java:177)
04-27 01:14:12.786: E/AndroidRuntime(25662): at android.os.AsyncTask$InternalHandler.handleMessage(AsyncTask.java:644)
04-27 01:14:12.786: E/AndroidRuntime(25662): at android.os.Handler.dispatchMessage(Handler.java:99)
04-27 01:14:12.786: E/AndroidRuntime(25662): at android.os.Looper.loop(Looper.java:137)
04-27 01:14:12.786: E/AndroidRuntime(25662): at android.app.ActivityThread.main(ActivityThread.java:5039)
04-27 01:14:12.786: E/AndroidRuntime(25662): at java.lang.reflect.Method.invokeNative(Native Method)
04-27 01:14:12.786: E/AndroidRuntime(25662): at java.lang.reflect.Method.invoke(Method.java:511)
04-27 01:14:12.786: E/AndroidRuntime(25662): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
04-27 01:14:12.786: E/AndroidRuntime(25662): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
04-27 01:14:12.786: E/AndroidRuntime(25662): at dalvik.system.NativeStart.main(Native Method)
.
stackoverflow.comm
No comments:
Post a Comment