0

i want to create my sqlite database but i always receive a nullpointer exception.

I start my Databasehelper this way:

ContentProvider cp = new ContentProvider(this, this);

Then

public ContentProvider(Context _context, MainActivity activity) {
    db = new DatabaseHelper(context);

And the Databasehelper looks like this

public class DatabaseHelper extends SQLiteOpenHelper {
private static final String DATENBANK_NAME = "serien.db";
private static final int DATENBANK_VERSION = 1;
// Table Names
private static final String TABLE_SERIEN = "serien";
private static final String TABLE_EPISODEN = "episoden";

// Common column names
private static final String KEY_ID = "id";

private static final String KEY_NAME = "name";
private static final String KEY_ISFAV = "isfav";
private static final String KEY_ENR = "episodennr";
private static final String KEY_SNR = "staffelnr";
private static final String KEY_SID = "serienid";
private static final String KEY_RELEASE = "release";

// serien table create statement
private static final String CREATE_TABLE_SERIEN = "CREATE TABLE "
        + TABLE_SERIEN + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME
        + " TEXT," + KEY_ISFAV + " INTEGER);";

// episoden table create statement
private static final String CREATE_TABLE_EPISODEN = "CREATE TABLE "
        + TABLE_EPISODEN + "(" + KEY_ID + " INTEGER PRIMARY KEY," + KEY_ENR + " INTEGER ," + 
        KEY_SNR + " INTEGER ," + KEY_SID + " INTEGER ," + KEY_ID + " TEXT, FOREIGN KEY("+ KEY_SID + ") REFERENCES "+TABLE_SERIEN+"("+KEY_ID+"));";

private Context appContext;

public DatabaseHelper(Context context) {
    super(context, DATENBANK_NAME, null, DATENBANK_VERSION);
    appContext = context;
} // Db()

@Override
 public void onCreate(SQLiteDatabase db) {
 Log.d("CREATE", CREATE_TABLE_SERIEN);
    Log.d("CREATE", CREATE_TABLE_EPISODEN);
 db.execSQL(CREATE_TABLE_SERIEN);
 db.execSQL(CREATE_TABLE_EPISODEN);
 } // onCreate()

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_SERIEN);
    db.execSQL("DROP TABLE IF EXISTS " + TABLE_EPISODEN);
    onCreate(db);
} // onUpgrade()

@Override
public void onOpen(SQLiteDatabase db) {
    super.onOpen(db);
    if (!db.isReadOnly()) {
        // Enable foreign key constraints
        db.execSQL("PRAGMA foreign_keys=ON;");
    }
}

I then want to write some data into the database with this method

public long createSerie(Serie serie) {
    DatabaseHelper dbHelper = new DatabaseHelper(appContext);
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_NAME, serie.getName());
    values.put(KEY_ISFAV, serie.getIsFav());

    // insert row
    long serie_id = db.insert(TABLE_SERIEN, null, values);

    return serie_id;

}

But here comes the problem. I always got an error at the line ..getWritableDatabase().

I've read a lot of different threads about this problem, but nothing i tryed worked.

Has anyone an idea what the problem could be?

Thank you very much!

Irazzer
  • 47
  • 1
  • 7

1 Answers1

1
public ContentProvider(Context _context, MainActivity activity) {
    db = new DatabaseHelper(context);

_context and context are different.

Likely context is uninitialized and therefore null. Passing a null for a Context to SQLiteOpenHelper causes NPE in getDatabaseLocked() when getWritableDatabase() is called.

Make sure you're passing a valid context to your database helper.

laalto
  • 150,114
  • 66
  • 286
  • 303