簡體   English   中英

“最終”塊之后的空指針異常

[英]Null Pointer Exception after a 'finally' block

在這段代碼的結尾,我得到了一個N​​ullPointerException:

 @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

這是錯誤的屏幕截圖:

錯誤

添加catch塊后,我得到了異常的實際stacktrace:

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)

感謝大家的幫助。

原來,我必須添加一個catch塊來獲取異常的實際StackTrace。

我發現這是從這條線來的:

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

所以很明顯我的extras.getString('message')為null。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM