Error when GetReadableDatabase fails

I created a DBTest SQLiteOpenHelper class

Then I called it from my main interface.

It crashes when I hit DB.GetReadableDatabase () and the log doesn't help, it just says a null pointer, but I don't know where to look.

Everything works if I use

SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

Same database

Here is a class for helper and below error:

  public class DBTest extends SQLiteOpenHelper { private static String DB_NAME = "DB"; private SQLiteDatabase myDataBase; private final Context myContext; public DBTest(Context context) { super(context, DB_NAME, null, 1); this.myContext = context; } @Override public void onCreate(SQLiteDatabase db) { // TODO Auto-generated method stub } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // TODO Auto-generated method stub } } 

The following is the error:

 public class Main extends Activity { DataBaseHelper db = new DataBaseHelper(null); static SQLiteDatabase Db; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); SQLiteDatabase Db; DBTest db1 = new DBTest(null); Db = db1.getReadableDatabase(); <<< blow up here } } 
+4
source share
1 answer
 DBTest db1 = new DBTest(this.getApplicationContext()); 

Context cannot be null

+4
source

Source: https://habr.com/ru/post/1335342/


All Articles