I have a Database full of alarms, the service to create the alarms starts on boot, however crashes once in the loop to create them. the error is:
"java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Context.getPackageName()' on a null object reference"
EDIT:
Code:
public class NotificationServiceStartOnBoot extends Service {
UserDbHelper userDbHelper;
SQLiteDatabase sqLiteDatabase;
Context context;
Cursor cursor;
@Override
public IBinder onBind(Intent intent) {
return null;
}
@Override
public void onCreate() {
super.onCreate();
Log.e("SERVICE", "STARTED...");
userDbHelper = new UserDbHelper(getApplicationContext());
sqLiteDatabase = userDbHelper.getReadableDatabase();
cursor = userDbHelper.getNotifications(sqLiteDatabase);
Log.e("SERVICE", "About to begin loop...");
if(cursor.moveToFirst())
{
Log.e("SERVICE", "if statement accepted...");
do
{
Log.e("SERVICE", "notification loop begun...");
String date,UID;
date = cursor.getString(0);
UID = cursor.getString(1);
// Long notificationDate = Long.valueOf(date);
Long notificationUID = Long.valueOf(UID);
long milliSeconds= Long.parseLong(date);
//System.out.println(milliSeconds);
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(milliSeconds);
final int _id = (int) System.currentTimeMillis();
Intent alarmIntent = new Intent(context, AlarmReceiver.class);
alarmIntent.putExtra("nID", notificationUID);
//make sure the intent have id's
PendingIntent pendingIntent = PendingIntent.getBroadcast(context, _id, alarmIntent , 0);
AlarmManager alarmManager = (AlarmManager)context.getSystemService(ALARM_SERVICE);
alarmManager.set(AlarmManager.RTC, calendar.getTimeInMillis(), pendingIntent);
}while (cursor.moveToNext());
}
Log.e("SERVICE", "outside if statement...");
//NotificationGenerator.generateNotification(context, null, null, null, null);
}
}
Your context is null. Context context;
where did you set your context ? Set your context before use it.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.