当我将数据插入数据库时,我从Log
收到以下错误12-15 13:50:59.174: D/AndroidRuntime(15829): Shutting down VM
12-15 13:50:59.174: W/dalvikvm(15829): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
12-15 13:50:59.184: E/AndroidRuntime(15829): FATAL EXCEPTION: main
12-15 13:50:59.184: E/AndroidRuntime(15829): java.lang.NullPointerException
12-15 13:50:59.184: E/AndroidRuntime(15829): at com.carettech.android.CaretTechActivity.inserting(CaretTechActivity.java:120)
12-15 13:50:59.184: E/AndroidRuntime(15829): at com.carettech.android.CaretTechActivity.adding(CaretTechActivity.java:107)
12-15 13:50:59.184: E/AndroidRuntime(15829): at com.carettech.android.CaretTechActivity$1.onClick(CaretTechActivity.java:55)
12-15 13:50:59.184: E/AndroidRuntime(15829): at android.view.View.performClick(View.java:2408)
12-15 13:50:59.184: E/AndroidRuntime(15829): at android.view.View$PerformClick.run(View.java:8816)
12-15 13:50:59.184: E/AndroidRuntime(15829): at android.os.Handler.handleCallback(Handler.java:587)
12-15 13:50:59.184: E/AndroidRuntime(15829): at android.os.Handler.dispatchMessage(Handler.java:92)
12-15 13:50:59.184: E/AndroidRuntime(15829): at android.os.Looper.loop(Looper.java:123)
12-15 13:50:59.184: E/AndroidRuntime(15829): at android.app.ActivityThread.main(ActivityThread.java:4627)
12-15 13:50:59.184: E/AndroidRuntime(15829): at java.lang.reflect.Method.invokeNative(Native Method)
12-15 13:50:59.184: E/AndroidRuntime(15829): at java.lang.reflect.Method.invoke(Method.java:521)
12-15 13:50:59.184: E/AndroidRuntime(15829): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
12-15 13:50:59.184: E/AndroidRuntime(15829): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
12-15 13:50:59.184: E/AndroidRuntime(15829): at dalvik.system.NativeStart.main(Native Method)
我用来插入的代码是:
SQLiteDatabase Database;
ContentValues values = new ContentValues();
values.put(NAME,a.getNAME());
values.put(EMAIL, a.getEmail());
values.put(PLACE,a.getPlace());
Database.insert(CARET_TECH_TABLE, null, values);
我也试过这个
String sql = "INSERT INTO "+CARET_TECH_TABLE+" ("+NAME+","+EMAIL+","+PLACE+") "
+"VALUES ('"+ a.getNAME() + "', '" + a.getEmail() + "', '" +a.getPlace()+"')";
Log.v("Test Saving", sql);
Database.execSQL(sql, null);
无法调试并找到问题...感谢您的帮助!
答案 0 :(得分:1)
您没有初始化数据库对象
SQLiteDatabase Database;
//在您用户之前,您必须创建数据库对象 //使用您正在创建方法等的Datatabse类
Database = new Database(context);
//或者在您使用之前必须创建数据库对象
SQLiteDatabase Database = context.openOrCreateDatabase("my_database_name", Context.MODE_WORLD_WRITEABLE, null);
ContentValues values = new ContentValues();
values.put(NAME,a.getNAME());
values.put(EMAIL, a.getEmail());
values.put(PLACE,a.getPlace());
Database.insert(CARET_TECH_TABLE, null, values);