我有以下DBAdapter(這不是全部,但它是相關的代碼)。Android SQLite - openOrCreateDatabase()nullPointerException
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)
看起來它越來越某種異常的,有關當幫手是決定是否要創建或更新數據庫。 ..
DATABASE_NAME是什麼值? – James
這只是「賣家」的常數。對不起,我沒有拋出所有常量,我會添加我需要的上述代碼。 –
這幾乎肯定是由'Context'傳入'null'引起的 - [''ContextWrapper.java:203'](http://grepcode.com/file/repository.grepcode.com/java' /ext/com.google.android/android/2.3.5_r1/android/content/ContextWrapper.java#ContextWrapper.openOrCreateDatabase%28java.lang.String%2Cint%2Candroid.database.sqlite.SQLiteDatabase.CursorFactory%29)可能會導致一個NPE。 –