Jesper Jacobsen
Jesper Jacobsen

Reputation: 161

NullPointerException in BroadcastReceiver

I am totally new to Android development, so I apologize before hand if what I'm asking is really simple, I've been Googleing around for the past couple of hours with no luck.

In my MainActivity I reference AlarmReceiver.class in an Intent and then into a PendingIntent and finally it is set to an AlarmManager. Which I do to set an alarm, and this works fine, it's when the alarm is triggered the Exception happens.

    Intent intent = new Intent(ourContext, AlarmReceiver.class);

    PendingIntent pendingIntent = PendingIntent.getBroadcast(ourContext, 666, intent, 0);

    AlarmManager alarmManager = (AlarmManager) ourContext.getSystemService(Context.ALARM_SERVICE);

    alarmManager.set(AlarmManager.RTC_WAKEUP, targetCal.getTimeInMillis(), pendingIntent);

And then in AlarmReceiver.class it extends BroadcastReceiver, and makes an SDK call. This part of the code in run whenever the Alarm triggers, meaning when the selected time is neigh, run this code.

public void onReceive(Context arg0, Intent arg1) {

    SDK.sendNotification(arg0, new Notification("alarm", "Top Line", "Bottom Line"), new SendNotificationListener() {

        @Override
        public void onSuccess(String s) {
            Log.d("success", s);
        }

        @Override
        public void onFailed(String s) {
            Log.d("fail", s);
        }
    });
}

SendNotification takes 3 variables, a context, a Notification and a NotificationListener.

What I've been able to determine, I am pretty sure that it's the NotificationListener that is the problem and causes the error.

Here is the stacktrace:

Process: com.example.timer:remote, PID: 27665
    java.lang.RuntimeException: Unable to start receiver com.example.timer.AlarmReceiver: java.lang.NullPointerException
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2452)
            at android.app.ActivityThread.access$1700(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.NullPointerException
            at com.example.imer.AlarmReceiver.onReceive(AlarmReceiver.java:21)
            at android.app.ActivityThread.handleReceiver(ActivityThread.java:2445)
            at android.app.ActivityThread.access$1700(ActivityThread.java:144)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1281)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5146)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:796)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:612)
            at de.robv.android.xposed.XposedBridge.main(XposedBridge.java:132)
            at dalvik.system.NativeStart.main(Native Method)

This is line 21:

SDK.sendNotification(arg0, new Notification("alarm", "Top Line", "Bottom Line"), new SendNotificationListener() {

Upvotes: 0

Views: 1486

Answers (1)

Stephen C
Stephen C

Reputation: 719679

If the exception is thrown here:

  SDK.sendNotification(arg0, 
                       new Notification("alarm", "Top Line", "Bottom Line"), 
                       new SendNotificationListener() {

then the only possible explanation is that SDK is null. Check that it is properly initialized; i.e. a non-null value has been assigned to it. (And check that you don't have multiple declarations ... and that some of them are not initialized.)


You posted this as (supposed) counter-evidence.

  SDK.initiate(getActivity(), "DEVCODE", new String[] { Scope }, 
               new AuthListener() {
  1. That doesn't set SDK to a non-null value. It doesn't set (hint: assign) anything to SDK.

    (But hypothetically, it would give an NPE if SDK is a variable, it is null and initiate is an instance method.)

  2. That statement may not be executed.

  3. Even if that SDK variable is not null, there could be another declaration of SDK.

In short, that doesn't prove anything.

Upvotes: 4

Related Questions