title: android BroadcastReceiver sometimes throws NullPointerException at onReceive
body: <p>I have a BroadcastReceiver that is called periodically from AlarmManager
  This is the BroadcastReceiver:</p>
  
  <pre><code>package com.panic.receiver;
  
  import com.panic.service.MyService;
  import com.panic.utils.Analytics;
  
  import android.app.AlarmManager;
  import android.app.PendingIntent;
  import android.content.BroadcastReceiver;
  import android.content.Context;
  import android.content.Intent;
  import android.os.PowerManager;
  import android.util.Log;
  
  public class KeepAliveReceiver extends BroadcastReceiver {
  final String TAG = "PeriodicUpdateReceiver";
  private static MyService mService;
  
  @Override
  public void onReceive(Context context, Intent intent) {
      try {
          Log.v(TAG, "onReceive");
          PowerManager pm = (PowerManager)mService.getSystemService(Context.POWER_SERVICE);
          PowerManager.WakeLock wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, "PanicButton");
          //Acquire the lock
          wl.acquire();
          mService.sendKeepAlive();
          //Release the lock
          wl.release();
      }
      catch(Exception ex) {
          Analytics.sendException("KeepAliveReceiver onReceive", ex, false);
      }
  
  }
  
  
  public void startPeriodicUpdates(MyService service, int time)
  {
      mService = service;
      AlarmManager am=(AlarmManager)mService.getSystemService(Context.ALARM_SERVICE);
      Intent intent = new Intent(mService, KeepAliveReceiver.class);
      PendingIntent pi = PendingIntent.getBroadcast(mService, 0, intent, 0);
      //After after 30 seconds
      am.setRepeating(AlarmManager.RTC_WAKEUP, System.currentTimeMillis(), time , pi); 
  }
  }
  </code></pre>
  
  <p>The receiver is called every hour, and it works well but sometimes (rarely) I get the following exception:</p>
  
  <pre><code>java.lang.RuntimeException: Unable to start receiver        com.ex.receiver.KeepAliveReceiver: java.lang.NullPointerException
  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2277)
  at android.app.ActivityThread.access$1500(ActivityThread.java:140)
  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:137)
  at android.app.ActivityThread.main(ActivityThread.java:4898)
  at java.lang.reflect.Method.invokeNative(Native Method)
  at java.lang.reflect.Method.invoke(Method.java:511)
  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1006)
  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:773)
  at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.NullPointerException
  at com.ex.receiver.KeepAliveReceiver.onReceive(KeepAliveReceiver.java:19)
  at android.app.ActivityThread.handleReceiver(ActivityThread.java:2270)
  ... 10 more
  </code></pre>
  
  <p>line 19 is the function header:
      public void onReceive(Context context, Intent intent) {</p>
  
  <p>Why is this happening?</p>