title: java.lang.RuntimeException: Failure delivering result ResultInfo{who=@android:requestPermissions:, request=10, result=-1, data=Intent
body: <p><strong>EDIT</strong>:</p>
  
  <p>Now the app is working. But it only asks for the permission after the first time it is started(after installation). If I leave the app(pressing back button) and then later start it again it doesn't ask for the permission. Why is this happening?</p>
  
  <p>Doesn't pressing the back button destroys the activity by calling onDestroy() and shouldn't it call onCreate() method again?</p>
  
  <p><strong>ORIGINAL QUESTION</strong></p>
  
  <p>My app allows the user to find their current location on the press of the button.Whenever the app is started for the first time, it asks for the user permission but it crashes when the permission is granted giving following error:</p>
  
  <pre><code>java.lang.RuntimeException: Failure delivering result 
  ResultInfo{who=@android:requestPermissions:, request=10, result=-1,
  data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has
  extras) }} to activity
  {com.example.hpi5.location/com.example.hpi5.location.MainActivity}:
  java.lang.NullPointerException: Attempt to invoke virtual method 'void
  android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
  on a null object reference
  </code></pre>
  
  <p>Below is my Activity code:</p>
  
  <pre><code>import android.location.Location;
  import android.location.LocationListener;
  import android.location.LocationManager;
  import android.Manifest;
  import android.os.Build;
  import android.os.Bundle;
  import android.support.annotation.NonNull;
  import android.support.v7.app.AppCompatActivity;
  import android.view.View;
  import android.content.pm.PackageManager;
  import android.widget.Button;
  import android.widget.Toast;
  
  public class MainActivity extends AppCompatActivity {
  
      Button button ;
      LocationListener locationListener;
      LocationManager locationManager;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
  
          if (Build.VERSION.SDK_INT &gt;=23 ){
              if (checkSelfPermission(Manifest.permission.ACCESS_FINE_LOCATION)
                      != PackageManager.PERMISSION_GRANTED) {
                  requestPermissions(new String[]{Manifest.permission.ACCESS_FINE_LOCATION},
                          10);
                  return;
              }
          }
  
          button = (Button) findViewById(R.id.button);
  
          locationManager = (LocationManager)
                  getSystemService(LOCATION_SERVICE);
  
          locationListener = new LocationListener()
          {
  
              @Override
              public void onStatusChanged(String provider, int status, Bundle extras) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onProviderEnabled(String provider) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onProviderDisabled(String provider) {
                  // TODO Auto-generated method stub
  
              }
  
              @Override
              public void onLocationChanged(Location location) {
                  // TODO Auto-generated method stub
                  double latitude = location.getLatitude();
                  double longitude = location.getLongitude();
                  //double speed = location.getSpeed(); //spe2d in meter/minute
                  //speed = (speed*3600)/1000;      // speed in km/minute
                  Toast.makeText(getApplicationContext(), "lat" + latitude + "lon" + longitude,Toast.LENGTH_SHORT).show();
              }
          };
  
  
  
  
      }
      void requestUpdate(){
          button.setOnClickListener(new View.OnClickListener(){
              @Override
              public void onClick(View view) {
                  locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
              }
          });
  
      }
  
      @Override
      public void onRequestPermissionsResult(int requestCode,
                                             @NonNull String permissions[], @ NonNull int[] grantResults) {
          switch (requestCode) {
              case -1:{
                  //hello
              }
  
              case 10: {
                  if (grantResults.length&gt;0 &amp;&amp; grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                      requestUpdate();
                  }
              }
  
          }
      }
  
  
  
  }
  </code></pre>
  
  <p>Below is the logcat error:</p>
  
  <pre><code>java.lang.RuntimeException: Failure delivering result
  ResultInfo{who=@android:requestPermissions:, request=10, result=-1,
  data=Intent { act=android.content.pm.action.REQUEST_PERMISSIONS (has
  extras) }} to activity
  {com.example.hpi5.location/com.example.hpi5.location.MainActivity}:
  java.lang.NullPointerException: Attempt to invoke virtual method 'void
      android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
       on a null object reference
                                                                               at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                             at
      android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                              at android.app.ActivityThread.-wrap16(ActivityThread.java)
       android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                           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)
  com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                          at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
                                                                           Caused by: java.lang.NullPointerException: Attempt to invoke virtual
       method 'void
    android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
       on a null object reference
  com.example.hpi5.location.MainActivity.requestUpdate(MainActivity.java:80)
       com.example.hpi5.location.MainActivity.onRequestPermissionsResult(MainActivity.java:95)
      android.app.Activity.dispatchRequestPermissionsResult(Activity.java:6553)
                                                                           at android.app.Activity.dispatchActivityResult(Activity.java:6432)
                                                                          at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
   android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                          at android.app.ActivityThread.-wrap16(ActivityThread.java) 
   android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                          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) 
       com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726) 
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
  </code></pre>