mercredi 23 septembre 2015

Cannot write to sqlite from another activity

I try to call a method and write to my database from another activity in android but my app crashes because it cannot write. My activity does not have On Create because it does not need it. Therefore I have this code: DB db = new DB(ServSearcher.this); which I want to give me acess to my Database and then I use my method below like:

List<Contact> contacts = db.getAllContacts();
for (Contact cn : contacts) {
    db.addContact(new Contact(var, getTime()));

Here is my logcat error:

java.lang.NullPointerException
        at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:268)
        at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
        at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
        at com.android.SecondActivity.DB.getAllContacts(DB.java:88)
        at com.android.SecondActivity.ServSearcher$5.onAvailable(ServSearcher.java:189)

I think it is obvious that my app cannot write to database so this line is wrong DB db = new DB(ServSearcher.this); or at least does not gives me access, any ideas?

Aucun commentaire:

Enregistrer un commentaire