I'm using using AsyncTask in getting my results.I get an an error within the doinbackground method saying it attempt to invoke method on a null object reference.
Here is my code private class PlaceListTask extends AsyncTask> {
private String strAddress;
public PlaceListTask(String strAddress) {
super();
this.strAddress = strAddress;
}
public String getStrAddress() {
return strAddress;
}
public void setStrAddress(String strAddress) {
this.strAddress = strAddress;
}
@Override
protected ArrayList<AutocompletePrediction> doInBackground(String... params) {
List<Place.Field> placeFields = Arrays.asList(Place.Field.ID, Place.Field.NAME /* add more Place.Field. fields as you need*/);
FindCurrentPlaceRequest request =
FindCurrentPlaceRequest.newInstance(placeFields);
Task<FindCurrentPlaceResponse> placeResponse = placesClient.findCurrentPlace(request);
placeResponse.addOnCompleteListener(task -> {
if (task.isSuccessful()) {
FindCurrentPlaceResponse response = task.getResult();
for (PlaceLikelihood placeLikelihood : response.getPlaceLikelihoods()) {
Log.i(TAG, String.format("Place '%s' has likelihood: %f",
placeLikelihood.getPlace().getName(),
placeLikelihood.getLikelihood()));
}
} else {
Exception exception = task.getException();
if (exception instanceof ApiException) {
ApiException apiException = (ApiException) exception;
Log.e(TAG, "Place not found: " + apiException.getStatusCode());
}
}
});
return null;
}
This is the error i get Attempt to invoke interface method 'com.google.android.gms.tasks.Task com.google.android.libraries.places.api.net.PlacesClient.findCurrentPlace(com.google.android.libraries.places.api.net.FindCurrentPlaceRequest)' on a null object reference