0

I am getting a NullPointerException int the following code.For the complete program refer http://wptrafficanalyzether.in/blog/showing-nearby-places-using-google-places-api-and-google-map-android-api-v2/.

Logcat

FATAL EXCEPTION: main
                                                                            Process: com.example.android.googleplaces, PID: 2813
                                                                            java.lang.NullPointerException
                                                                                at com.example.android.googleplaces.MainActivity$ParserTask.onPostExecute(MainActivity.java:200)
                                                                                at com.example.android.googleplaces.MainActivity$ParserTask.onPostExecute(MainActivity.java:169)

and code:

class ParserTask extends AsyncTask<String, Integer, List<HashMap<String,String>>>{

JSONObject jObject;

// Invoked by execute() method of this object
@Override
protected List<HashMap<String,String>> doInBackground(String... jsonData) {

List<HashMap<String, String>> places = null;
PlaceJSONParser placeJsonParser = new PlaceJSONParser();

try{
jObject = new JSONObject(jsonData[0]);

/** Getting the parsed data as a List construct */
places = placeJsonParser.parse(jObject);

}catch(Exception e){
Log.d("Exception",e.toString());
}
return places;
}

// Executed after the complete execution of doInBackground() method
@Override
protected void onPostExecute(List<HashMap<String,String>> list){

// Clears all the existing markers
if(mGoogleMap!=null)
mGoogleMap.clear();

for(int i=0;i<list.size();i++){

// Creating a marker
MarkerOptions markerOptions = new MarkerOptions();

// Getting a place from the places list
HashMap<String, String> hmPlace = list.get(i);

// Getting latitude of the place
double lat = Double.parseDouble(hmPlace.get("lat"));

// Getting longitude of the place
double lng = Double.parseDouble(hmPlace.get("lng"));

// Getting name
String name = hmPlace.get("place_name");

// Getting vicinity
String vicinity = hmPlace.get("vicinity");

LatLng latLng = new LatLng(lat, lng);

// Setting the position for the marker
markerOptions.position(latLng);

// Setting the title for the marker.
//This will be displayed on taping the marker
markerOptions.title(name + " : " + vicinity);

// Placing a marker on the touched position
if(mGoogleMap!=null)
mGoogleMap.addMarker(markerOptions);
}
}
}
justDroid
  • 343
  • 2
  • 9
hushan
  • 63
  • 5

2 Answers2

2

Well If its from mGoogleMap.addMarker(markerOptions); line(and you are sure that markeroptions variable is not null) it means that you are adding the marker while the map is not ready ... you should call the asyncTask once the map is loaded as the below code :

    mGoogleMap.setOnMapLoadedCallback(new GoogleMap.OnMapLoadedCallback() {
                @Override
                public void onMapLoaded() {
                    //start async task here 
                }
            });

Hope this will help.

M.Khouli
  • 3,992
  • 1
  • 23
  • 26
1

Change this line

List<HashMap<String, String>> places = null;

with

List<HashMap<String, String>> places = new ArrayList<HashMap<String, String>>();

Hope it will solve your problem.

KishuDroid
  • 5,411
  • 4
  • 30
  • 47