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 ?

  • You must get the base object itself with the 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
  • @pavlofff Tell me please, I correctly understand that I must first create a dbHelper object. And then through this object it will connect to the database through the methods you get getWritableDatabase () / getReadableDatabase (). - Alexander
  • Yes, that is right . - pavlofff
  • But look, I'm trying to access an already created object in MyApplication - dbHelper = MyApplication.getInstance (). GetDbHelper (). And already at this stage, the application crashes with a NullPointerException error. It turns out that ContentProvider is created before MyApplication? - Alexander

0