Null pointer exception reports in C2DMBaseReceiver class (from marketplace)
<p>I have C2DM implemented in my app, I see a decent number of crash reports in the marketplace caused by a null pointer exception in the C2DMBaseReceiver class. This class is from the chrometophone project, which is referenced in the C2DM guide (http://code.google.com/android/c2dm/):</p>

<p><a href="http://code.google.com/p/chrometophone/source/browse/trunk/android/c2dm/com/google/android/c2dm/C2DMBaseReceiver.java" rel="nofollow">http://code.google.com/p/chrometophone/source/browse/trunk/android/c2dm/com/google/android/c2dm/C2DMBaseReceiver.java</a></p>

<p>The npe happens in onHandleIntent(), mWakeLock is sometimes null:</p>

<pre><code>// From C2DMBaseReceiver.java:
@Override
public final void onHandleIntent(Intent intent) {
    try {
        Context context = getApplicationContext();
        if (intent.getAction().equals(REGISTRATION_CALLBACK_INTENT)) {
            handleRegistration(context, intent);
        } else if (intent.getAction().equals(C2DM_INTENT)) {
            onMessage(context, intent);
        } else if (intent.getAction().equals(C2DM_RETRY)) {
            C2DMessaging.register(context, senderId);
        }
    } finally {
        //  Release the power lock, so phone can get back to sleep.
        // The lock is reference counted by default, so multiple 
        // messages are ok.

        // If the onMessage() needs to spawn a thread or do something else,
        // it should use it's own lock.
        //
        //
        //
        // NULL POINTER EXCEPTION REPORTS HERE
        mWakeLock.release();
        //
        //
        //
    }
}
</code></pre>

<p>The fix is easy enough, just check for mWakeLock != null before accessing it. But I wonder if anyone else has seen this, if the docs should be updated with this check? Or maybe this is a bigger problem, if the author expected mWakeLock to always be initialized?</p>

<p>Thanks</p>

<hr>

<p>I put a null check in that line, just moved the error a bit:</p>

<pre><code>java.lang.RuntimeException: WakeLock under-locked C2DM_LIB
  at android.os.PowerManager$WakeLock.release(PowerManager.java:304)
  at android.os.PowerManager$WakeLock.release(PowerManager.java:279)
  at com.google.android.c2dm.C2DMBaseReceiver.onHandleIntent(C2DMBaseReceiver.java:122)
  at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:59)
  at android.os.Handler.dispatchMessage(Handler.java:99)
  at android.os.Looper.loop(Looper.java:123)
  at android.os.HandlerThread.run(HandlerThread.java:60)
</code></pre>

<p>Still no idea how to fix this.</p>
