Null pointer exception when checking for permission with android.content.Context.checkPermission
<p>I need to check for permissions before querying the Android calendar for events.
To do it, Android studio is warning that I need to follow a check before querying.
The auto generated code is this piece:</p>

<pre><code>if (ActivityCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {
        System.out.println("NO ACCESS TO CALENDAR!! Abort mission, abort mission!!");
    }
</code></pre>

<p>When trying to run it, I get this error: </p>

<blockquote>
  <p>Attempt to invoke virtual method 'int<br>
      android.content.Context.checkPermission(java.lang.String, int, int)' 
      on a null object reference</p>
</blockquote>

<p>So it is clear that something is null at this point, and I tried to get the context of the app with a different way, but it's still the same error. 
Other thing I tried was this code, which is supposed to handle the targets lower than Android 6:</p>

<pre><code>if (ContextCompat.checkSelfPermission(this, Manifest.permission.READ_CALENDAR) != PackageManager.PERMISSION_GRANTED) {

        ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.READ_CALENDAR},
                MY_PERMISSIONS_REQUEST_READ_CONTACTS);
    }
</code></pre>

<p>Still get the same error, can anybody help me with this?</p>