-1

After force upgrading dabs in my android application I'm getting the following error from some devices:

Caused by java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.getVersion()' on a null object reference at com.readystatesoftware.sqliteasset.SQLiteAssetHelper.getWritableDatabase(SQLiteAssetHelper.java:178)

Have anybody ever faced with this kind of problem and how you solved it? Thank you!

UPD: Adding the implementation of my DBManager class:

public class DBManager {

    private SQLiteDatabase db;
    private Context context;

    public DBManager(Context context) {
        DBHelper dbHelper = new DBHelper(context);
        db = dbHelper.getWritableDatabase();
        this.context = context;
}

private class DBHelper extends SQLiteAssetHelper {
    private static final String DB_NAME = "database.db";
            private static final int DATABASE_VERSION = 5;

        public DBHelper(Context context) {
            super(context, DB_NAME, null, DATABASE_VERSION);
            setForcedUpgrade(5);
        }


    }
}
  • The NPE is not in your code so the linked dupe does not really help. But do post how you're setting up your SQLiteAssetHelper. Also if you can repro this yourself, there's likely some prior log that tells you in more detail what went wrong, such as "could not open database" with an exception message. – laalto Oct 18 '17 at 04:49
  • @laalto the problem is I could't reproduce this bug. But there is a lot of crash logs on Fabric's crashlytics. – Mukhamed Issa Oct 18 '17 at 05:02
  • got any solution ? i have same issue :( – KamDroid Dec 12 '18 at 05:31
  • @Kamwave yep. Before, the initialization of DBManager was in Application class. Then I decided to make a singleton from DBManager and remove initialization in Application. `public static DBManager getInstance(Context context) { if (sInstance == null) { synchronized (DBManager.class) { if (sInstance == null) { context = context.getApplicationContext(); sInstance = new DBManager(context); } } } return sInstance; }` – Mukhamed Issa Dec 13 '18 at 08:02

1 Answers1

2

The error is caused by bad code in SQLiteAssetHelper at line 413, where the horrendous error handling is logging the error ("could not open database") and returning null.

If you look at your logcat, you should see the error, and if you fix it, code will work.

The implementation of SQLiteAssetHelper is still bad, though.

Andreas
  • 138,167
  • 8
  • 112
  • 195