'finally'块后的空指针异常

时间:2013-05-07 10:58:27

标签: java android nullpointerexception google-cloud-messaging try-catch-finally

我在这段代码的末尾得到一个NullPointerException:

 @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
    }

这是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

这是错误的屏幕截图:

error

添加catch块后,我得到了异常的实际堆栈跟踪:

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)

1 个答案:

答案 0 :(得分:0)

谢谢大家的帮助。

原来我必须添加一个catch块来获取异常的实际StackTrace。

我发现它来自这条线:

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

很明显,我的extras.getString('message')为空。