GetWritableDatabase returns null

I have an Android service that uses a SQLite database. I ran into some problems with initializing the database.

I have a DBHelper class:

public class DBHelper extends SQLiteOpenHelper{
public boolean db_loaded=false;
public DBHelper() {
    super(ContextBean.getLocalContext(), "resplugin1.db", null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL("create table tasks ("
            + "id integer primary key autoincrement,"
            + "title text);");
    db_loaded=true;
}

I have a client class that uses it:

public class DataProvider {
private SQLiteDatabase database;
private DBHelper dbHelper;

public DataProvider(){
    dbHelper=new DBHelper();
    database = dbHelper.getWritableDatabase();
}
public void close(){
    dbHelper.close();
}
public TaskDescriptor getTaskById(int id){
    while(dbHelper.db_loaded==false){}
    String[] args={Integer.toString(id)};
    if(database==null){
        Log.d("ResPluginService1","DATABASE IS NULL");
    }
/*...*/
}

The problem is that the database object is always NULL . The getWritableDatabase method does not throw exceptions; it simply returns null. What can cause such strange behavior?

+4
source share

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


All Articles