打开数据库时,外部空指针异常

时间:2011-08-13 17:08:06

标签: java android sqlite

我的应用中出现了一个奇怪的错误。使用后我将它留在后台(使用其他应用程序),然后我尝试返回我的应用程序。获取DBAdapter对象的调用因空指针异常而失败。这是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

代码的一些小脚:在MainUI.java onResume():

  DBAdapter db= DBAdapter.getInstance();

在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;
}

最罕见的是查看源代码方法

  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 {
        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();
        }
    }
}

第118行(至少在我的源代码中对应

success = true;

因此对空指针异常毫无意义

提前致谢

编辑:

发现问题谢谢。这是它

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper() {
        super(Manager.appcontext, DB_NAME, null,DB_VERSION);
    }

我使用静态变量来存储appcontext但是在被OS杀死后变量为null

2 个答案:

答案 0 :(得分:1)

DatabaseHelper中的内容是什么?您需要Context来初始化SQLiteOpenHelper,因此很可能是null。

答案 1 :(得分:0)

你在Eclipse上运行吗?

当错误与行号不对应时,我会刷新代码并清理应用程序。

要刷新,请右键单击项目并选择刷新。

主菜单项目下可以使用清洁。