I have a database helper class dbHelper which does the usual operation on the database. I have an activity which opens uses the database for its operation. Now i want to edit the database through a service. So I implemented the code below and it gives a null point exception error. When i removed all the code in onHandleIntent and put a log it compiled. But I dont understand why there is null point exception here. The database exists as it has been used through the activity.
Logcat says Null point exception at DbHelper.getAllItems(). I dont understand as to why there should be an exception in the Service class and not in the activity class.
public class ItemStateService extends IntentService {
public static final String C_NAME = "item";
static final String TAG = "ItemStateService";
public ItemStateService() {
super(TAG);
}
protected void onHandleIntent(Intent intent) {
DbHelper dbHelper= new DbHelper (this);
Cursor cursor;
cursor = dbHelper.getAllItems();
int count = 0;
for(cursor.moveToFirst(); !cursor.isAfterLast(); cursor.moveToNext()) {
count++;
}
Log.d(TAG, "dbHelper launched with item count "+count);
}
}