NullPointerException with checkSelfPermission – Am I looking in the right place?

I have released my Android app but I am seeing a lot of the following errors from users. I cannot reproduce the issue myself every time on the emulator so it’s hard for me to debug it myself.

It appears to happen when the user opens my app after a while of being in the background. App will crash on first run. When the user opens the app again everything is fine. I feel it is something to do with either my permission or context being null. I do not have too much experience reading nullpointerexeptions so would love some help!

Error:

java.lang.NullPointerException: 
  at android.support.v4.content.ContextCompat.checkSelfPermission (ContextCompat.java:425)
  at me.XXXXXXX.XXXXXXXX.DistanceFragment.getDeviceLocation (DistanceFragment.java:463)
  at me.XXXXXXX.XXXXXXXX.DistanceFragment.onConnected (DistanceFragment.java:239)
  at com.google.android.gms.common.internal.zzm.zzq (Unknown Source)
  at com.google.android.gms.internal.zzaal.zzo (Unknown Source)
  at com.google.android.gms.internal.zzaaj.zzvE (Unknown Source)
  at com.google.android.gms.internal.zzaaj.onConnected (Unknown Source)
  at com.google.android.gms.internal.zzaan.onConnected (Unknown Source)
  at com.google.android.gms.internal.zzzy.onConnected (Unknown Source)
  at com.google.android.gms.common.internal.zzl$1.onConnected (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zzj.zzwZ (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zza.zzc (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zza.zzu (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zze.zzxa (Unknown Source)
  at com.google.android.gms.common.internal.zzf$zzd.handleMessage (Unknown Source)
  at android.os.Handler.dispatchMessage (Handler.java:102)
  at android.os.Looper.loop (Looper.java:154)
  at android.app.ActivityThread.main (ActivityThread.java:6682)
  at java.lang.reflect.Method.invoke (Native Method)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

Am I right in saying that…

DistanceFragment.onConnected (DistanceFragment.java:239) is called first
---> then calls DistanceFragment.getDeviceLocation (DistanceFragment.java:463)
------> checkSelfPermission (ContextCompat.java:425)*

Check permission has 2 parameters:

context Context
permission String:

One of these is null?

Code below for these lines:

/// Line 239

@Override
public void onConnected(Bundle connectionHint) {

    getDeviceLocation(); /// Line 239

/// Line 463

   if (ContextCompat.checkSelfPermission(getContext(),
                android.Manifest.permission.ACCESS_FINE_LOCATION)
                == PackageManager.PERMISSION_GRANTED) {
            mLocationPermissionGranted = true;
        } else {
            ActivityCompat.requestPermissions(getActivity(),
                    new String[]{android.Manifest.permission.ACCESS_FINE_LOCATION},
                    PERMISSIONS_REQUEST_ACCESS_FINE_LOCATION);
        }

Thanks for your help.

If anyone had a solution on how to ensure this is not null, it would be greatly appreciated.

Am I right in saying that… One of these is null?

Yes.

If anyone had a solution on how to ensure this is not null, it would be greatly appreciated.

Fix your fragment. Only be registered with Play Services while the fragment is attached to an activity. Unregister from Play Services at other times, such as when the fragment is detached. Right now, your onConnected() callback is being called sometime when the fragment is not attached to an activity, and so getActivity() is returning null.