Catalin Ghita
Catalin Ghita

Reputation: 826

String becomes null outside of Anonymous Inner Class

I am retrieving the LastKnowlocation with google's FusedLocationClient and storing userLocality and userCountry as two strings in OnsuccesListener anonymous inner class.

In the code below: the text set is correct in the locationProvidedTextView(therefore userLocality and userCountry get some values) but when I try to print the string values outside of the inner class they somehow both become null.

It might be a stupid question, but what am I doing wrong? I need to use those values in another OnclickListener method.

Code snippet:

    // GlOBAL VARIABLES
   ....
   private  String userCountry;
   private  String userLocality;

@Override
protected void onCreate(Bundle savedInstanceState) {
      ....
 mFusedLocationClient.getLastLocation()
            .addOnSuccessListener(this, new OnSuccessListener<Location>() {
                @Override
                public void onSuccess(Location location) {
                    // Got last known location. In some rare situations this can be null.
                    if (location != null) {
                        Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                        try {
                            List<Address> addresses = geoCoder
        .getFromLocation(location.getLatitude(), location.getLongitude(), 1);

                            if (addresses.size() > 0) {
                                if (addresses.size() > 0) {
                                     userLocality = addresses.get(0).getLocality();
                                     userCountry = addresses.get(0).getCountryName();

                                    locationProvidedTextView.setText("Your address: " + userLocality + ", " + userCountry);

                                }
                            }
                        } catch (IOException e1) {
                            e1.printStackTrace();
                        }

                    }
                }
            });
 // HERE BOTH STRINGS BECOME NULL
    System.out.println("ADDRESS1 ======= " + userLocality);
    System.out.println("ADDRESS2 ======= " + userCountry);
          ....
   }

Upvotes: 0

Views: 256

Answers (4)

Akshat Vajpayee
Akshat Vajpayee

Reputation: 324

            @Override
            public void onSuccess(Location location) {
                // Got last known location. In some rare situations this can be null.
                if (location != null) {
                    Geocoder geoCoder = new Geocoder(getBaseContext(), Locale.getDefault());
                    try {
                        List<Address> addresses = geoCoder
    .getFromLocation(location.getLatitude(), location.getLongitude(), 1);

                        if (addresses.size() > 0) {
                            if (addresses.size() > 0) {
                                 userLocality = addresses.get(0).getLocality();
                                 userCountry = addresses.get(0).getCountryName();

                                locationProvidedTextView.setText("Your address: " + userLocality + ", " + userCountry);

                            }
                        }
                    } catch (IOException e1) {
                        e1.printStackTrace();
                    }

I guess this method set data to userLocality and userCountry each time when it gets the cordinates based on the GPS accurately but when GPS is disable or network is not strong it can give false reading which may result in getting null String variables here you should try changing the provider and the accuracy of the network or gps provider this might help.

Upvotes: 0

Bhuvanesh BS
Bhuvanesh BS

Reputation: 13617

Obtaining location will take some time. As per your code you are printing immediately mFusedLocationClient.getLastLocation().addOnSuccessListener

Your listener won't get called immediately. So you won't get any value in those strings.

A better way of implementation is, do all the location related thing in another class, not in onCreate. Then use interface to get the result once you got it.

Upvotes: 2

Geoffrey
Geoffrey

Reputation: 1241

This is normal.

I don't know your code however it seems that addOnSuccessListener is an asynchronous mechanism.

So you want to display outside the asynchronous part the value which has not been set.

In other words you try to display a value which has not been set.

To test this you can put: Inside the addOnSuccessListener a:

System.out.println("User locality found");

You will see that message is probably comming after the

System.out.println("ADDRESS1 ======= " + userLocality);

Upvotes: 1

Murat Karag&#246;z
Murat Karag&#246;z

Reputation: 37594

The issue is that you did not build any mechanism for retrieving the values. In other words your onCreate looks like this right now:

onCreate{
// set listener
// print stuff out
}

The values are getting set after the listener gets called, but at that point you are already done with the onCreate method.

Upvotes: 1

Related Questions