Android SQLite - openOrCreateDatabase()nullPointerException

时间:2011-10-31 18:28:25

标签: android sqlite

我有以下DBAdapter(这不是全部,但它是相关的代码)。

private final String DATABASE_NAME = "seller";
private final String DATABASE_MY_TABLE = "sellertable";
private final Integer DATABASE_VERSION = 1
private final String CREATE_TABLE = "CREATE TABLE seller ........";

public DBAdapter( Context c )
{
    this.context = c;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper
{
    public DatabaseHelper( Context context )
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate( SQLiteDatabase db )
    {
        MyLog.d("Creating Tables");
        try
        {
            db.execSQL(CREATE_TABLE);
        }
        catch (Exception e)
        {
            e.printStackTrace();
        }
    }

    @Override
    public void onUpgrade( SQLiteDatabase db, int oldVersion, int newVersion )
    {
        MyLog.w("Upgrading database from version " + oldVersion + " to " + newVersion + ". Destroying old data.");
        db.execSQL("DROP TABLE IF EXISTS " + DATABASE_MY_TABLE);
        onCreate(db);
    }
}

// Opens the database
public DBAdapter open() throws SQLException
{
    db = DBHelper.getWritableDatabase();
    return this;
}

当我尝试db.open()时,我得到以下内容:

FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to instantiate application com.company.app.MyApp: java.lang.NullPointerException
    at android.app.LoadedApk.makeApplication(LoadedApk.java:466)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:3260)
    at android.app.ActivityThread.access$2200(ActivityThread.java:117)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:969)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:130)
    at android.app.ActivityThread.main(ActivityThread.java:3683)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:507)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
    at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
    at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:203)
    at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:118)
    at com.company.app.DBAdapter.open(DBAdapter.java:74)
    at com.company.app.MyApp.<init>(MyApp.java:106)
    at java.lang.Class.newInstanceImpl(Native Method)
    at java.lang.Class.newInstance(Class.java:1409)
    at android.app.Instrumentation.newApplication(Instrumentation.java:957)
    at android.app.Instrumentation.newApplication(Instrumentation.java:942)
    at android.app.LoadedApk.makeApplication(LoadedApk.java:461)

看来它正在获得某种异常,与帮助者决定是否创建或更新数据库有关......

3 个答案:

答案 0 :(得分:5)

我刚刚碰到这个,可能是出于其他原因。在onCreate()被调用之前,我尝试做一些与db相关的事情,涉及一个上下文。希望这有助于那里的人

答案 1 :(得分:3)

尝试在数据库名称的末尾添加“.db”等扩展名。

答案 2 :(得分:0)

如果确切的问题出现在Context上,我可以在昨天面对同样的问题时给出建议。

假设我从其他类调用 DatabaseHelper ,那么我必须提供上下文。我对哪个上下文感到困惑: 1. getApplicationContext() 2. getBaseContext() 3.已在类的构造函数中初始化的Context实例,该类正在调用DatabaseHelper对象,即_context。

但我得到的实际解决方案是使用这个

例如: -

DatabaseHelper dbHelper = new DatabaseHelper( this );