Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NullPointerException when trying to check permissions

Tags:

I'm trying to check if the user has android.Manifest.permission.ACCESS_FINE_LOCATION enabled. When running the application, I get a NullPointerException error.

package dtt.romano.rsrpechhulp;

import android.app.Activity; import android.content.Context; import android.content.IntentSender; import android.content.pm.PackageManager; import android.location.Location; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.app.ActivityCompat; import android.util.Log;  import com.google.android.gms.common.ConnectionResult; import com.google.android.gms.common.api.GoogleApiClient; import com.google.android.gms.location.LocationListener; import com.google.android.gms.location.LocationRequest; import com.google.android.gms.location.LocationServices;  public class LocationProvider extends MainActivity implements     GoogleApiClient.ConnectionCallbacks,     GoogleApiClient.OnConnectionFailedListener,LocationListener {  public abstract interface LocationCallback {     public void handleNewLocation(Location location); }  public static final String TAG = LocationProvider.class.getSimpleName(); private final static int CONNECTION_FAILURE_RESOLUTION_REQUEST = 9000;  private LocationCallback mLocationCallback; private Context mContext; private GoogleApiClient mGoogleApiClient; private LocationRequest mLocationRequest;  public LocationProvider(Context context, LocationCallback callback) {     mGoogleApiClient = new GoogleApiClient.Builder(context)             .addConnectionCallbacks(this)             .addOnConnectionFailedListener(this)             .addApi(LocationServices.API)             .build();      mLocationCallback = callback;      // Create the LocationRequest object     mLocationRequest = LocationRequest.create()             .setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY)             .setInterval(10 * 1000)        // 10 seconds, in milliseconds             .setFastestInterval(1 * 1000); // 1 second, in milliseconds      mContext = context; }  public void connect() {     mGoogleApiClient.connect(); }  public void disconnect() {     if (mGoogleApiClient.isConnected()) {         LocationServices.FusedLocationApi.removeLocationUpdates(mGoogleApiClient, this);         mGoogleApiClient.disconnect();     } }  public void locationUpdate(){     if (ActivityCompat.checkSelfPermission(this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {         ActivityCompat.requestPermissions(this, new String[]{                 android.Manifest.permission.ACCESS_FINE_LOCATION         }, 10);     }else{         Location location = LocationServices.FusedLocationApi.getLastLocation(mGoogleApiClient);         if (location == null) {             LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this);         }         else {             mLocationCallback.handleNewLocation(location);         }     } }  @Override public void onConnected(Bundle bundle) {     Log.i(TAG, "Location services connected.");     locationUpdate(); }  @Override public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {     switch (requestCode) {         case 10:             if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {                 locationUpdate();             }             return;     } }  @Override public void onConnectionSuspended(int i) {  }  @Override public void onConnectionFailed(ConnectionResult connectionResult) {     /*      * Google Play services can resolve some errors it detects.      * If the error has a resolution, try sending an Intent to      * start a Google Play services activity that can resolve      * error.      */     if (connectionResult.hasResolution() && mContext instanceof Activity) {         try {             Activity activity = (Activity)mContext;             // Start an Activity that tries to resolve the error             connectionResult.startResolutionForResult(activity, CONNECTION_FAILURE_RESOLUTION_REQUEST);         /*          * Thrown if Google Play services canceled the original          * PendingIntent          */         } catch (IntentSender.SendIntentException e) {             // Log the error             e.printStackTrace();         }     } else {         /*          * If no resolution is available, display a dialog to the          * user with the error.          */         Log.i(TAG, "Location services connection failed with code " + connectionResult.getErrorCode());     } }  @Override public void onLocationChanged(Location location) {     mLocationCallback.handleNewLocation(location); } } 

Here is the error log:

java.lang.NullPointerException: Attempt to invoke virtual method 'int android.content.Context.checkPermission(java.lang.String, int, int)' on a null object reference                                                            at android.content.ContextWrapper.checkPermission(ContextWrapper.java:637)  at android.support.v4.content.ContextCompat.checkSelfPermission(ContextCompat.java:387) at dtt.romano.rsrpechhulp.LocationProvider.onConnected(LocationProvider.java:68) at com.google.android.gms.common.internal.zzk.zzk(Unknown Source) at com.google.android.gms.common.api.internal.zzj.zzi(Unknown Source) at com.google.android.gms.common.api.internal.zzh.zzpx(Unknown Source) at com.google.android.gms.common.api.internal.zzh.onConnected(Unknown Source) at com.google.android.gms.common.api.internal.zzl.onConnected(Unknown Source) at com.google.android.gms.common.api.internal.zzc.onConnected(Unknown Source) at com.google.android.gms.common.internal.zzj$zzg.zzqL(Unknown Source) at com.google.android.gms.common.internal.zzj$zza.zzc(Unknown Source) at com.google.android.gms.common.internal.zzj$zza.zzw(Unknown Source) at com.google.android.gms.common.internal.zzj$zzc.zzqN(Unknown Source) at com.google.android.gms.common.internal.zzj$zzb.handleMessage(Unknown Source) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:148) at android.app.ActivityThread.main(ActivityThread.java:5417) at java.lang.reflect.Method.invoke(Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 
like image 584
B. Bob Avatar asked May 19 '16 12:05

B. Bob


2 Answers

if (ActivityCompat.checkSelfPermission((Activity)mContext, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) {     ActivityCompat.requestPermissions((Activity)mContext, new String[]{             android.Manifest.permission.ACCESS_FINE_LOCATION     }, 10); } 

Try to use (Activity)mContext instead of this. it may help.

like image 66
Nitin Jain Avatar answered Sep 23 '22 21:09

Nitin Jain


if (ActivityCompat.checkSelfPermission(ActivityName.this, android.Manifest.permission.ACCESS_FINE_LOCATION) != PackageManager.PERMISSION_GRANTED) { ActivityCompat.requestPermissions(ActivityName.this, new String[]{         android.Manifest.permission.ACCESS_FINE_LOCATION }, 10); 

}

Try with your ActivityName.this

like image 23
Joel George Avatar answered Sep 23 '22 21:09

Joel George