0

hey guys my method to return my current(last known) location is killing my app. My error logs aren't showing up, so the only think I can think of is that something is going on with the permissions/Location class. I have it set to pull my location if it can, else return a pre stored LatLng. Please help! here's my code

public LatLng getLocation(GoogleApiClient client) {

    if(client == null){
        Log.d(null, "error:null client");
    }else{
        if( ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED && ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_COARSE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
            Location mLastLocation = FusedLocationApi.getLastLocation(client);
            return new LatLng(mLastLocation.getLongitude(), mLastLocation.getLatitude());
        }else{
            Log.d(null, "error:permission fail");
        }
    }
return harrys;
}

and here is my error stack

java.lang.NullPointerException: Attempt to invoke virtual method 'double android.location.Location.getLongitude()' on a null object reference at com.example.kevin.memories.MapsActivity.getLocation(MapsActivity.java:251) at com.example.kevin.memories.MapsActivity.onMapReady(MapsActivity.java:93) at com.google.android.gms.maps.SupportMapFragment$zza$1.zza(Unknown Source) at com.google.android.gms.maps.internal.zzo$zza.onTransact(Unknown Source) at android.os.Binder.transact(Binder.java:387) at wv.a(:com.google.android.gms.DynamiteModulesB:82) at maps.ag.t$5.run(Unknown Source) at android.os.Handler.handleCallback(Handler.java:739) at android.os.Handler.dispatchMessage(Handler.java:95) at android.os.Looper.loop(Looper.java:158) at android.app.ActivityThread.main(ActivityThread.java:7224) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)

kevinb
  • 17
  • 9

2 Answers2

0

null object reference means you're citing an object that doesn't exist yet. So this may mean that your call to that object came first before the object's initialization. Try logging your getLastLocation if it was indeed able to fetch something.

Also check this SO thread for additional answers.

Community
  • 1
  • 1
ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
0

Make sure you call getLastLocation(client) only after GoogleApiClient gets Connected.you can check it by using client.isConnected() method or you can call it from onConnected method.