I have an Application Database in SQLite. Database creation takes place in MyApplication:
@Override public void onCreate() { super.onCreate(); sInstance = this; initDBHelper(); initaliseDataBase(); } private void initDBHelper(){ dbHelper = new DBHelper(this); } In DBHelper itself:
@Override public void onCreate(SQLiteDatabase db) { NoteTable.createTable(db); } And finally in NoteTable:
public static void createTable(SQLiteDatabase sqLiteDatabase){ sqLiteDatabase.execSQL("CREATE TABLE " + TABLE_NAME + " (" + COLUMN_ID + " integer primary key autoincrement," + COLUMN_DATE_CREATE + " text," + COLUMN_DATE_EDIT + " text," + COLUMN_AUTHOR + " text," + COLUMN_TITLE + " text," + COLUMN_BODY_NOTE + " text);"); } At the moment I am writing a ContentProvider , in which I get access to the created database in the following way:
@Override public boolean onCreate() { Log.d(LOG_TAG, "onCreate"); dbHelper = MyApplication.getInstance().getDbHelper(); return true; } As a result, the application crashes with a NullPointerException error in this term - dbHelper = MyApplication.getInstance().getDbHelper(); It turns out that the database does not have time to create? Is it not necessary to write the code to create the database in ContentProvider ?
getWritableDatabase()/getReadableDatabase()method. Where you do it is not important, but you can work with the database only through an object that returns one of these methods. - pavlofff