0

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

drey_1
  • 45
  • 10

1 Answers1

0

Before you call placesClient.findCurrentPlace(request) check if the placesClient is null.

Monim Kaiser
  • 65
  • 1
  • 2
  • 7
  • its connected but fails to find any of the location – drey_1 Feb 26 '20 at 09:01
  • 1. Are you using a real device? 2. Did you initialise placeClient like this: **placesClient = Places.createClient(context)** – Monim Kaiser Feb 26 '20 at 09:27
  • I'm using a real device and the api is initialized successfully but when i search it logs place not found which shouldn't be – drey_1 Feb 26 '20 at 10:02
  • Try `FindCurrentPlaceRequest findCurrentPlaceRequest = FindCurrentPlaceRequest.builder(placeFields).build();` Instead of: `FindCurrentPlaceRequest request = FindCurrentPlaceRequest.newInstance(placeFields);` – Monim Kaiser Feb 26 '20 at 10:09
  • It didn't work.I'm just trying to convert the deprecated one to new place api.That's the reason I'm having such issues – drey_1 Feb 26 '20 at 10:15