Question

I get a NullPointerException at the end of this piece of code :

 @Override
    public final void onHandleIntent(Intent intent) {
        try {
            Context context = getApplicationContext();
            String action = intent.getAction();
            if (action.equals(INTENT_FROM_GCM_REGISTRATION_CALLBACK)) {
                handleRegistration(context, intent);
            } else if (action.equals(INTENT_FROM_GCM_MESSAGE)) {
                // checks for special messages
                String messageType =
                        intent.getStringExtra(EXTRA_SPECIAL_MESSAGE);
                if (messageType != null) {
                    if (messageType.equals(VALUE_DELETED_MESSAGES)) {
                        String sTotal =
                                intent.getStringExtra(EXTRA_TOTAL_DELETED);
                        if (sTotal != null) {
                            try {
                                int total = Integer.parseInt(sTotal);
                                Log.v(TAG, "Received deleted messages " +
                                        "notification: " + total);
                                onDeletedMessages(context, total);
                            } catch (NumberFormatException e) {
                                Log.e(TAG, "GCM returned invalid number of " +
                                        "deleted messages: " + sTotal);
                            }
                        }
                    } else {
                        // application is not using the latest GCM library
                        Log.e(TAG, "Received unknown special message: " +
                                messageType);
                    }
                } else {
                    onMessage(context, intent);
                }
            } else if (action.equals(INTENT_FROM_GCM_LIBRARY_RETRY)) {
                String token = intent.getStringExtra(EXTRA_TOKEN);
                if (!TOKEN.equals(token)) {
                    // make sure intent was generated by this class, not by a
                    // malicious app.
                    Log.e(TAG, "Received invalid token: " + token);
                    return;
                }
                // retry last call
                if (GCMRegistrar.isRegistered(context)) {
                    GCMRegistrar.internalUnregister(context);
                } else {
                    GCMRegistrar.internalRegister(context, mSenderId);
                }
            }
        } 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 onMessage() needs to spawn a thread or do something else,
            // it should use its own lock.
            synchronized (LOCK) {
                // sanity check for null as this is a public method
                if (sWakeLock != null) {
                    Log.v(TAG, "Releasing wakelock");
                    if(sWakeLock.isHeld()){
                        sWakeLock.release();
                    }
                } else {
                    // should never happen during normal workflow
                    Log.e(TAG, "Wakelock reference is null");
                }
            }
        } // NullPointerException apparently thrown here
    }

This is the LogCat :

05-07 12:55:06.775: V/GCMBroadcastReceiver(19555): onReceive: com.google.android.c2dm.intent.RECEIVE
05-07 12:55:06.775: V/GCMBroadcastReceiver(19555): GCM IntentService class: com.predictoo.whimbee.GCMIntentService
05-07 12:55:06.775: V/GCMBaseIntentService(19555): Acquiring wakelock
05-07 12:55:06.815: D/GCMIntentService(19555): onMessage - context: android.app.Application@41aecd40
05-07 12:55:06.815: V/GCMBaseIntentService(19555): Releasing wakelock

This is a screenshot of the error :

error

After adding a catch block, I got the actual stacktrace of the exception :

05-07 14:15:38.216: W/System.err(26532): java.lang.NullPointerException: println needs a message
05-07 14:15:38.226: W/System.err(26532):    at android.util.Log.println_native(Native Method)
05-07 14:15:38.226: W/System.err(26532):    at android.util.Log.v(Log.java:119)
05-07 14:15:38.226: W/System.err(26532):    at com.predictoo.whimbee.GCMIntentService.onMessage(GCMIntentService.java:63)
05-07 14:15:38.226: W/System.err(26532):    at com.google.android.gcm.GCMBaseIntentService.onHandleIntent(GCMBaseIntentService.java:179)
05-07 14:15:38.226: W/System.err(26532):    at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
05-07 14:15:38.236: W/System.err(26532):    at android.os.Handler.dispatchMessage(Handler.java:99)
05-07 14:15:38.236: W/System.err(26532):    at android.os.Looper.loop(Looper.java:137)
05-07 14:15:38.236: W/System.err(26532):    at android.os.HandlerThread.run(HandlerThread.java:60)
Was it helpful?

Solution

Thank you everyone for your help.

Turned out I had to add a catch block to get the actual StackTrace of the exception.

I found out it was coming from this line :

Log.v(ME + ":onMessage extras ", extras.getString("message"));

So obviously my extras.getString('message') was null.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top