我正试着用我的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-27 23:16:07.698: W/System.err(4685): java.lang.NullPointerException
02-27 23:16:07.698: W/System.err(4685): at com.manager.boot.r1223.VSDataSource.open(VSDataSource.java:34)
02-27 23:16:07.698: W/System.err(4685): at com.manager.boot.r1223.VSDataSource.ScanVirtualSystems(VSDataSource.java:52)
02-27 23:16:07.706: W/System.err(4685): at com.manager.boot.r1223.OSListActivity.onCreate(OSListActivity.java:49)
02-27 23:16:07.706: W/System.err(4685): at android.app.Activity.performCreate(Activity.java:4465)
02-27 23:16:07.706: W/System.err(4685): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
02-27 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
02-27 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
02-27 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.access$600(ActivityThread.java:123)
02-27 23:16:07.706: W/System.err(4685): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
02-27 23:16:07.706: W/System.err(4685): at android.os.Handler.dispatchMessage(Handler.java:99)
02-27 23:16:07.706: W/System.err(4685): at android.os.Looper.loop(Looper.java:137)
02-27 23:16:07.706: W/System.err(4685): at android.app.ActivityThread.main(ActivityThread.java:4424)
02-27 23:16:07.706: W/System.err(4685): at java.lang.reflect.Method.invokeNative(Native Method)
02-27 23:16:07.706: W/System.err(4685): at java.lang.reflect.Method.invoke(Method.java:511)
02-27 23:16:07.706: W/System.err(4685): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
02-27 23:16:07.706: W/System.err(4685): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
02-27 23:16:07.706: W/System.err(4685): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
我认为这个问题是静态的。试试没有它。
我的例子:
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);
...
}