Skip to content Skip to sidebar Skip to footer

Null Pointer Creating Sqlite Database

Why do these two lines of code throw a NPE? SQLiteDatabase db; db = openOrCreateDatabase('TestingData.db', SQLiteDatabase.CREATE_IF_NECESSARY, null); Stacktrace: 08-23 10:33:14.28

Solution 1:

Your ContextWrapper does not have a base Context. Most likely you are not calling this method at the correct place, but I cannot say more without a little more code.

Solution 2:

The nice thing about Android is that it is open source. So we can see that openOrCreateDatabase() is implemented like:

publicSQLiteDatabaseopenOrCreateDatabase(String name, int mode, CursorFactory factory) {
    return mBase.openOrCreateDatabase(name, mode, factory);
}

The only way I can see this code throwing a NPE is if mBase is null. From your calling convention I assume your class is derived from android.content.ContextWrapper? If so, are you ensuring that the base context (mBase) is set when your class is instantiated? If you aren't, then that would be what's causing the problem.

Post a Comment for "Null Pointer Creating Sqlite Database"