title: Android - Checking Permissions in a Separate Class
body: <p>I found some other topics that touched on this, but the solutions did not work for me.</p>
  
  <p>I have a class <code>PermissionsManager.java</code> which contains the methods for checking permission, and I make a call to it in the <code>MainActivity.java</code>. I read from some people that I have to pass a Context and Activity to <code>checkPermissions()</code>, but I tried that although the Context allow my app to work if the permissions were already granted, the app would crash if permission had not been already granted. I had trouble passing a suitable Activity.</p>
  
  <p>I could just put all the code in my MainActivity and I know it works there since I had it all working, but I'd really like to set up my code in a MVC pattern to keep Views separate from controllers.</p>
  
  <p>I get the following error: </p>
  
  <blockquote>
    <p>java.lang.NullPointerException: Attempt to invoke virtual method 'int
    android.content.Context.checkPermission(java.lang.String, int, int)'
    on a null object reference</p>
  </blockquote>
  
  <p>Could anyone shed some light on how to get this to work?</p>
  
  <p><strong>PermissionsManager</strong></p>
  
  <pre><code>import android.Manifest;
  import android.content.DialogInterface;
  import android.content.pm.PackageManager;
  import android.os.Build;
  import android.support.v7.app.AlertDialog;
  import android.support.v7.app.AppCompatActivity;
  
  import java.util.ArrayList;
  import java.util.List;
  
  /**
   * Created on 5/6/2017.
   */
  
  public class PermissionsManager extends AppCompatActivity {
      final private int REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS = 124;
  
      public void checkPermissions() {
          List&lt;String&gt; permissionsNeeded = new ArrayList&lt;String&gt;();
  
          final List&lt;String&gt; permissionsList = new ArrayList&lt;String&gt;();
          if (!addPermission(permissionsList, Manifest.permission.READ_CONTACTS))
              permissionsNeeded.add("Read Contacts");
          if (!addPermission(permissionsList, Manifest.permission.SEND_SMS))
              permissionsNeeded.add("Send SMS Texts");
          if (!addPermission(permissionsList, Manifest.permission.RECEIVE_SMS))
              permissionsNeeded.add("Receive SMS Texts");
          if (!addPermission(permissionsList, Manifest.permission.READ_SMS))
              permissionsNeeded.add("Read SMS Texts");
  
  
          if (permissionsList.size() &gt; 0) {
              if (permissionsNeeded.size() &gt; 0) {
                  // Need Rationale
                  String message = "This app requires the following permissions to function correctly: \n" + permissionsNeeded.get(0);
                  for (int i = 1; i &lt; permissionsNeeded.size(); i++)
                      message = message + "\n" + permissionsNeeded.get(i);
                  showMessageOKCancel(message,
                          new DialogInterface.OnClickListener() {
                              @Override
                              public void onClick(DialogInterface dialog, int which) {
                                  if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                                      requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                                              REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
                                  }
                              }
                          });
                  return;
              }
              if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
                  requestPermissions(permissionsList.toArray(new String[permissionsList.size()]),
                          REQUEST_CODE_ASK_MULTIPLE_PERMISSIONS);
              }
              return;
          }
      }
  
      private void showMessageOKCancel(String message, DialogInterface.OnClickListener okListener) {
          new AlertDialog.Builder(this)
                  .setMessage(message)
                  .setPositiveButton("OK", okListener)
                  .setNegativeButton("Cancel", null)
                  .create()
                  .show();
      }
  
      private boolean addPermission(List&lt;String&gt; permissionsList, String permission) {
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              if (checkSelfPermission(permission) != PackageManager.PERMISSION_GRANTED) {
                  permissionsList.add(permission);
                  // Check for Rationale Option
                  if (!shouldShowRequestPermissionRationale(permission))
                      return false;
              }
          }
          return true;
      }
  }
  </code></pre>
  
  <p><strong>MainActivity OnCreate Method</strong></p>
  
  <pre><code>@Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
          Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
          setSupportActionBar(toolbar);
  
          // Check Permissions
          if (Build.VERSION.SDK_INT &gt;= Build.VERSION_CODES.M) {
              PermissionsManager permissionsManager = new PermissionsManager();
              permissionsManager.checkPermissions();
          }
      }
  </code></pre>
  
  <p>I tried some code like this, but it didn't work for me.</p>
  
  <pre><code>if(ContextCompat.checkSelfPermission((Activity)mContext,Manifest.permission.REA
          D_SMS)!=PackageManager.PERMISSION_GRANTED)    
         {
              ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.READ_SMS},
                          MY_PERMISSIONS_REQUEST_READ_SMS);
         }
  </code></pre>