Extrange null pointer exception when opening a database
I have a strange error in my app. When after using it I leave it in background (for using another apps) and then I attempt to return to my app. The call for getting a DBAdapter object fails with a null pointer exception. Here is the stacktrace
ERROR/AndroidRuntime(21804): FATAL EXCEPTION: main
ERROR/AndroidRuntime(21804): java.lang.RuntimeException: Unable to resume activity {com.newproject/com.newproject.ui.MainGalleryUI}: java.lang.NullPointerException
ERROR/AndroidRuntime(21804): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2124)
ERROR/AndroidRuntime(21804): at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:2139)
ERROR/AndroidRuntime(21804): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1672)
ERROR/AndroidRuntime(21804): at android.app.ActivityThread.access$1500(ActivityThread.java:117)
ERROR/AndroidRuntime(21804): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935)
ERROR/AndroidRuntime(21804): at android.os.Handler.dispatchMessage(Handler.java:99)
ERROR/AndroidRuntime(21804): at android.os.Looper.loop(Looper.java:130)
ERROR/AndroidRuntime(21804): at android.app.ActivityThread.main(ActivityThread.java:3687)
ERROR/AndroidRuntime(21804): at java.lang.reflect.Method.invokeNative(Native Method)
ERROR/AndroidRuntime(21804): at java.lang.reflect.Method.invoke(Method.java:507)
ERROR/AndroidRuntime(21804): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:842)
ERROR/AndroidRuntime(21804): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
ERROR/AndroidRuntime(21804): at dalvik.system.NativeStart.main(Native Method)
ERROR/AndroidRuntime(21804): Caused by: java.lang.NullPointerException
ERROR/AndroidRuntime(21804): at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
ERROR/AndroidRuntime(21804): at com.newproject.services.DBAdapter.open(DBAdapter.java:98)
ERROR/AndroidRuntime(21804): at com.newproject.services.DBAdapter.getInstance(DBAdapter.java:85)
ERROR/AndroidRuntime(21804): at com.newproject.ui.MainUI.onResume(MainUI.java:51)
ERROR/AndroidRuntime(21804): at android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1150)
ERROR/AndroidRuntime(21804): at android.app.Activity.performResume(Activity.java:3832)
ERROR/AndroidRuntime(21804): at android.app.ActivityThread.performResumeActivity(ActivityThread.java:2114)
ERROR/AndroidRuntime(21804): ... 12 more
08-13 18:32:02.660: ERROR/(477): Dumpstate > /data/log/dumpstate_app_error
And some snipplets of the code: At MainUI.java onResume():
DBAdapter db= DBAdapter.getInstance();
At DBAdapter:
private DatabaseHelper mDbHelper;
public static DBAdapter instance;
public static DBAdapter getInstance(){
if (instance==null){
instance=new DBAdapter();
instance.open();
}
return instance;
}
public DBAdapter(){
}
private synchronized DBAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper();
mDb = mDbHelper.getWritableDatabase();
return this;
}
And the rarest thing is that looking into the source code the method
public synchronized SQLiteDatabase getWritableDatabase() {
if (mDatabase != null && mDatabase.isOpen() && !mDatabase.isReadOnly()) {
return mDatabase; // The database is already open for business
}
if (mIsInitializing) {
throw new IllegalStateException("getWritableDatabase called recursively");
}
// If we have a read-only database open, someone could be using it
// (though they shouldn't), which would cause a lock to be held on
// the file, and our attempts to open the database read-write would
// fail waiting for the file lock. To prevent that, we acquire the
// lock on the read-only database, which shuts out other users.
boolean success = false;
SQLiteDatabase db = null;
if (mDatabase != null) mDatabase.lock();
try {
开发者_StackOverflow中文版 mIsInitializing = true;
if (mName == null) {
db = SQLiteDatabase.create(null);
} else {
db = mContext.openOrCreateDatabase(mName, 0, mFactory);
}
int version = db.getVersion();
if (version != mNewVersion) {
db.beginTransaction();
try {
if (version == 0) {
onCreate(db);
} else {
onUpgrade(db, version, mNewVersion);
}
db.setVersion(mNewVersion);
db.setTransactionSuccessful();
} finally {
db.endTransaction();
}
}
onOpen(db);
success = true;
return db;
} finally {
mIsInitializing = false;
if (success) {
if (mDatabase != null) {
try { mDatabase.close(); } catch (Exception e) { }
mDatabase.unlock();
}
mDatabase = db;
} else {
if (mDatabase != null) mDatabase.unlock();
if (db != null) db.close();
}
}
}
The line 118 (at least in my source code corresponds with
success = true;
So its senseless for a null pointer exception
Thanks in advance
Edit:
Found the problem thanks. Here is it
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper() {
super(Manager.appcontext, DB_NAME, null,DB_VERSION);
}
I was using an static variable for store the appcontext but after beeing killed by the OS the variable is null
What's in DatabaseHelper
? You need a Context
to initialize a SQLiteOpenHelper
, so that's most likely what's null.
Are you running on Eclipse?
When errors don't line up with line numbers, I would Refresh the code and Clean the app.
To refresh, right-click on the project and choose refresh.
Clean is available under the main menu Project.
精彩评论