0

My application is deigned to start a BroadcastReceiver when the phone boots up, however i get a NullPointerException. I'm not sure if I'm setting up the receivers properly.

The error i got when booting up the device, using the adb logcat command:

12-01 09:46:12.542  6465  6465 E AndroidRuntime: FATAL EXCEPTION: main
12-01 09:46:12.542  6465  6465 E AndroidRuntime: Process: com.idkbro.pashchallenge, PID: 6465
12-01 09:46:12.542  6465  6465 E AndroidRuntime: java.lang.RuntimeException: Unable to instantiate receiver com.idkbro.pashchallenge.BootReceiver: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2992)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.app.ActivityThread.-wrap18(ActivityThread.java)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1544)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.os.Handler.dispatchMessage(Handler.java:102)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.os.Looper.loop(Looper.java:154)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.app.ActivityThread.main(ActivityThread.java:6077)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at java.lang.reflect.Method.invoke(Native Method)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)
12-01 09:46:12.542  6465  6465 E AndroidRuntime: Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.content.ComponentName.<init>(ComponentName.java:128)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.content.Intent.<init>(Intent.java:4868)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at com.idkbro.pashchallenge.BootReceiver.<init>(BootReceiver.java:24)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at java.lang.Class.newInstance(Native Method)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        at android.app.ActivityThread.handleReceiver(ActivityThread.java:2987)
12-01 09:46:12.542  6465  6465 E AndroidRuntime:        ... 8 more 

The BootReceiver class:

public class BootReceiver extends BroadcastReceiver {

@Override
public void onReceive(Context context, Intent intent) {

    if (intent.getAction().equals("android.intent.action.BOOT_COMPLETED")){
        Log.i("Xu", "Boot receiver is working");


    }

  }
}

The Manifest for it:

<receiver
        android:name=".BootReceiver"
        android:enabled="true">
        <intent-filter>
            <action android:name="android.intent.action.BOOT_COMPLETED" />

        </intent-filter>
    </receiver>
Raghunandan
  • 132,755
  • 26
  • 225
  • 256
Fay12Keh32
  • 27
  • 1

0 Answers0