Search code examples
androidbroadcastreceiveralarmmanagerandroid-broadcastandroid-alarms

Getting null pointer when using intent.action.BOOT_COMPLETED


I got null pointer exception when try to implement a sample code from here. I have some confusing about the sample code there in "Start the alarm when the device boots" section in point 2 and point 3. Here are my main class to trigger the alarm and my broadcast receiver class to receive the broadcast and trigger an action, and my manifest file. My logcat null pointer exception error point to the if block statement in broadcast receiver class. My question is :

  1. Am I correctly implementing code from the sample to my project? If not, please give me a correct one. Because I don't want the alarm to be canceled when the device reboot. I will appreciate any help, thank you.

Solution

  • Change:

    if(intent.getAction().equals("android.intent.action.BOOT_COMPLETED"))
    

    to:

    if("android.intent.action.BOOT_COMPLETED".equals(intent.getAction()))
    

    so it handles null action strings properly. That should clear up the crash that you are experiencing.