我使用下面的代碼打開 SQLite 數據庫:
dbHelper = new DataBaseHelper(context);
public static void open() throws SQLException {
try {
database = dbHelper.getWritableDatabase();
} catch (Exception e) {
e.printStackTrace();
Log.d(Tag.getTag("open()"), "Can't open db");
}
}
但是獲得 NullPointerException 異常。下面是 DataBaseHelper 類
//declarations and stuff
public DataBaseHelper(Context context) {
super(context, VFS_DATABASE_NAME, null, DATABASE_VERSION);
this.context = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
Toast.makeText(context, "Creating DataBase for first time...", 2000).show();
db.execSQL(DATABASE_CREATE);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(DataBaseHelper.class.getName(),
"Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS" + VFS_DATABASE_TABLE);
onCreate(db);
}
我找不出哪裡出錯。這個錯誤是如何發生的呢?
堆棧跟蹤:
02-20 23:16:07.698: W/System.err(4685): java.lang.NullPointerException
02-20 23:16:07.698: W/System.err(4685): at com.manager.boot.r1223.VSDataSource.open(VSDataSource.java:34)
02-20 23:16:07.698: W/System.err(4685): at com.manager.boot.r1223.VSDataSource.ScanVirtualSystems(VSDataSource.java:52)
02-20 23:16:07.706: W/System.err(4685): at com.manager.boot.r1223.OSListActivity.onCreate(OSListActivity.java:49)
02-20 23:16:07.706: W/System.err(4685): at android.app.Activity.performCreate(Activity.java:4465)
02-20 23:16:07.706: W/System.err(4685): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-20 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-20 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-20 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-20 23:16:07.706: W/System.err(4685): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-20 23:16:07.706: W/System.err(4685): at android.os.Handler.dispatchMessage(Handler.java:99)
02-20 23:16:07.706: W/System.err(4685): at android.os.Looper.loop(Looper.java:137)
02-20 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-20 23:16:07.706: W/System.err(4685): at java.lang.reflect.Method.invokeNative(Native Method)
02-20 23:16:07.706: W/System.err(4685): at java.lang.reflect.Method.invoke(Method.java:511)
02-20 23:16:07.706: W/System.err(4685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-20 23:16:07.706: W/System.err(4685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-20 23:16:07.706: W/System.err(4685): at dalvik.system.NativeStart.main(Native Method)
我認為是 static 中的問題,去掉它。
例如:
public class MyActivity extends ListActivity {
private DatabaseHandler db;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list);
db = new DatabaseHandler(this);
...
}