Search code examples
androidandroid-pendingintentalarm

PendingIntent null when trying to call it from a class


Im bulding an alarm clock, and I got a pendingIntent that should snooze the alarm. The same code worked great when it was in the Activity. I change some things, and now its in a different class. I get a nullpointerExecption. (The contractor gets the context from the activity.

Snooze function:

public void settingNewIntentForSnooze() {
    PendingIntent alarmPendingIntent=null;
    Calendar calendar = Calendar.getInstance();
    // new alarm after the snooze.
    calendar.set(Calendar.MINUTE, calendar.get(Calendar.MINUTE) + snoozeTime);
    Intent snoozeIntent = new Intent(context, Alarm_Reciver.class);
    snoozeIntent.putExtra("click_status", true);
    //add that to try to solve the problem...
    snoozeIntent.setAction("Snooze");
    alarmPendingIntent = PendingIntent.getBroadcast(context, 0, snoozeIntent, alarmPendingIntent.FLAG_ONE_SHOT);
    //tells to phone to set the alarm
    alarm_manager.set(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), alarmPendingIntent);
}

AlarmPage activity(the code that creats the variable) :

snooze= new Snooze(this,alarm_manager);

AlarmReciver:

public void onReceive(Context context, Intent intent) {
    Log.e("in alarm reciver","in alarm reciver");
    Intent service_intent= new Intent(context, RingtonePlayingService.class);//intent for the service ringtone playing
    boolean button_status = intent.getExtras().getBoolean("click_status");
    service_intent.putExtra("click_status", button_status);//passing button status
    context.startService(service_intent);//start the ringtone service
}

The error:

09-17 18:25:25.564 1563-1563/com.example.itay.newfrindlyalarm E/AndroidRuntime: FATAL EXCEPTION: main
                                                                            Process: com.example.itay.newfrindlyalarm, PID: 1563
                                                                            java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.AlarmManager.set(int, long, android.app.PendingIntent)' on a null object reference
                                                                                at com.example.itay.newfrindlyalarm.Snooze.settingNewIntentForSnooze(Snooze.java:60)
                                                                                at com.example.itay.newfrindlyalarm.Snooze.operate(Snooze.java:44)
                                                                                at com.example.itay.newfrindlyalarm.AlarmPage.operateSnooze(AlarmPage.java:88)
                                                                                at com.example.itay.newfrindlyalarm.AlarmPage$3.onClick(AlarmPage.java:68)
                                                                                at android.view.View.performClick(View.java:6256)
                                                                                at android.view.View$PerformClick.run(View.java:24697)
                                                                                at android.os.Handler.handleCallback(Handler.java:789)
                                                                                at android.os.Handler.dispatchMessage(Handler.java:98)
                                                                                at android.os.Looper.loop(Looper.java:164)
                                                                                at android.app.ActivityThread.main(ActivityThread.java:6541)
                                                                                at java.lang.reflect.Method.invoke(Native Method)
                                                                                at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
                                                                                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
enter code here

Solution

  • Judging by the error message, is it possible that alarm_manager is null? If you initialize AlarmManager properly, does the error go away?