title: Getting Location out of a LocationResult in onReceive()
body: <p>My App uses <strong>LocationServices</strong> to request location updates. It sends the results via a <strong>PendingIntent</strong> to a <strong>Receiver</strong>. In <strong>onReceive</strong>()  a <strong>LocationResult</strong> is extracted  from the intent, which yields a <strong>Location</strong> that is then expressed in terms of <strong>Lat</strong> and <strong>Lng</strong>, to be used further. Sounds OK, right?</p>
  
  <p>Well, the App started to crash with <strong>NullPointerException</strong>.</p>
  
  <p>I investigated using <strong>Debugger</strong> and found that the three lines of code which I described above executed fine, but then, instead of proceeding to the following lines of code, execution was somehow directed back again to the first of the three lines, which extracts a <strong>LocationResult</strong> from an intent. This now turned out to be null, and crashed on the following line with <strong>NullPointerException</strong>, as  <strong>LocationResult</strong> was assumed not to be null.</p>
  
  <p>I have no idea why this is happening. Any pointers, please? (Sorry for the pun)</p>
  
  <p>Here is my code:</p>
  
  <pre><code>public class MyLocationUpdateReceiver extends BroadcastReceiver {
  
      private static final String TAG = "MyLocationUpdateRcvr";
  
   // empty constructor
      public MyLocationUpdateReceiver(){}
  
      @Override
      public void onReceive(Context context, Intent intent) {
  
          Log.i(TAG,"onReceive update");
  
          // extract locationResult from intent received from Update Request
          LocationResult locationResult = LocationResult.extractResult(intent);
          double locationLat = locationResult.getLastLocation().getLatitude();
          double locationLng = locationResult.getLastLocation().getLongitude();
  
          // send the Lat and Lng of new location to MapActivity via intent
          Intent mIntent = new Intent(context.getApplicationContext(), 
          MapActivity.class);
          mIntent.putExtra("New_Lat", locationLat);
          mIntent.putExtra("New_Lng", locationLng);
          mIntent.addFlags(FLAG_ACTIVITY_NEW_TASK);
          context.startActivity(mIntent);
  
      }
  }
  </code></pre>
  
  <p>And here is the screenshot of the first time through.Note the LocationResult is not null.</p>
  
  <p><a href="https://i.stack.imgur.com/9GUhY.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/9GUhY.png" alt="enter image description here"></a></p>
  
  <p>Still on the first time round...</p>
  
  <p><a href="https://i.stack.imgur.com/x999f.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/x999f.png" alt="enter image description here"></a></p>
  
  <p>and on the second time round...</p>
  
  <p><a href="https://i.stack.imgur.com/762Vq.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/762Vq.png" alt="enter image description here"></a></p>