Alarm Manager Error NullPointerException

I'm doing the timer app, but I don't understand why the program error. this my code

public class SampleBootReceiver extends BroadcastReceiver {
    private PendingIntent pendingIntent;
    AlarmManager alarmManager;
    @Override

    public void onReceive(Context context, Intent intent) {

        DatabaseHandler db ;
        db=new DatabaseHandler(context);
    Contact cn=    db.getContact(1);

        Calendar calendar = Calendar.getInstance();
        calendar.set(Calendar.HOUR_OF_DAY, Integer.parseInt(cn.getHour()));
        calendar.set(Calendar.MINUTE, Integer.parseInt(cn.getMinutes()));
        calendar.set(Calendar.DATE, Integer.parseInt(cn.getDay()));
        Intent myIntent = new Intent(context,AlarmReceiver.class);
        Log.w("log", "HOURS" +Integer.parseInt(cn.getHour()));
        Log.w("log", "MINUTE" +Integer.parseInt(cn.getMinutes()));
        Log.w("log", "DAY" +Integer.parseInt(cn.getDay()));
        Log.w("log", "id" + cn.getId());
        Log.w("log", "phone" + cn.getPhoneNumber());
        Log.w("log", "sms" + cn.getSmsBody());

        pendingIntent = PendingIntent.getBroadcast(context, 0, myIntent, pendingIntent.FLAG_UPDATE_CURRENT);
        alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20000, pendingIntent);

    }
}

And this is Error NullPointerException in the alarmManager's line alarmManager.setRepeating(AlarmManager.RTC_WAKEUP, calendar.getTimeInMillis(), 20000, pendingIntent);

Upvotes: 0

Views: 425

Answers (1)

Claudio Redi
Claudio Redi

Reputation: 68440

So far I see on your code, you never initialize alarmManager so it's null when you call setRepeating.

Before calling this method you should have something like this

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

Upvotes: 1

Related Questions