0

I'm getting a crash when trying to open an SQLiteDatabase I have for writing with a Null Pointer Exception. Here's how I instantiate the SQLiteOpenHelper:

private final OpenHelper mOpenHelper;

public static DataManager getInstance() {
    if (sInstance == null) {
        sInstance = new DataManager();
    }
    return sInstance;
}

private DataManager() {
    mOpenHelper = new OpenHelper(MainApp.getInstance(), MainApp.getInstance().getWalletToken());
    SQLiteDatabase.loadLibs(MainApp.getInstance());
}

where MainApp is my application object.

Here's what I'm trying to do:

    SQLiteDatabase db = null;
    try {
        db = mOpenHelper.getWritableDatabase();

        for (Card card : cards) {
            if (card == null) {
                continue;
            }

            addCard(card, db);

            ContentValues values = new ContentValues();
            values.put(DataEntry.COLUMN_NAME_NICKNAME, card.getNickname());

            File imageFile = getImageFile(Integer.toString(card.getIndex()));
            if (ImageUtils.saveImage(card.getCardImage(), imageFile)) {
                values.put(DataEntry.COLUMN_NAME_IMAGE_FILE, imageFile.getAbsolutePath());
            }

            String whereClause = DataEntry.COLUMN_NAME_CARD_INDEX + " = "
                    + card.getIndex();
            db.update(DataEntry.TABLE_NAME, values, whereClause, null);
        }
    } finally {
        if (db != null) {
            db.close();
        }
    }        

And here's the crash output:

03-10 16:49:10.533: E/AndroidRuntime(8771): FATAL EXCEPTION: main
03-10 16:49:10.533: E/AndroidRuntime(8771): java.lang.NullPointerException
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteDatabase.<init>(SQLiteDatabase.java:1957)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteDatabase.openDatabase(SQLiteDatabase.java:901)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteDatabase.openOrCreateDatabase(SQLiteDatabase.java:944)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at net.sqlcipher.database.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:107)
03-10 16:49:10.533: E/AndroidRuntime(8771):     at com.mywebsite.DataManager$OpenHelper.getWritableDatabase(DataManager.java:282)

I googled for the source code but that particular line doesn't contain anything that should throw an exception. What am I doing wrong?

mpellegr
  • 3,072
  • 3
  • 22
  • 36
  • How did you initialize `mOpenHelper` ? – gunar Mar 10 '14 at 21:00
  • 1
    Can you show your initialization of mOpenHelper? – ucsunil Mar 10 '14 at 21:02
  • Is that second section of code part of the CardDataManager or is it somewhere else? – Nathaniel D. Waggoner Mar 10 '14 at 21:28
  • Your mOpenHelper object is resolving to null. Have you tried debugging to see if the wallet token value is returned properly. Because like CommonsWare answered, you are getting an instance of the MainApp properly which means that the only other possibility is the getWalletToken() method. – ucsunil Mar 10 '14 at 21:30

1 Answers1

1

I'd call loadLibs() before new OpenHelper().

Beyond that, my guess is that getWalletToken() is returning null. MainApp.getInstance() is not returning null, otherwise you would be crashing on the getWalletToken() call itself. How is the user providing this "wallet token"?

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • getWalletToken() was indeed returning null. I did not think this would be a problem but thank you for showing me it is! – mpellegr Mar 11 '14 at 22:17