0

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); 
}
}
4

1 回答 1

0

替换这一行:

DbHelper dbHelper= new DbHelper (this);

有了这个:

DbHelper dbHelper= new DbHelper (ItemStateService.this);
于 2013-01-14T06:51:19.913 回答