我在更新此第 59 行的表时不断收到空指针异常,即
myDB.execSQL(updatequery);
注意:在活动 1 中,构造函数有 3 个编辑文本字段,其中第一个 2 个字段具有值,第三个保持为空。一旦第二个活动开始,第三个字段就会在同一个表中更新。
我正在像这样在活动 1 中插入数据
dba.createddb(data11, data22, null);
以下是我更新数据的活动
public class Activity2 extends Activity {
EditText ed3;
Button btn3;
SQLiteDatabase myDB;
DBAdapter dba;
private String mRowId;
String data3;
String updatequery;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.act2);
ed3=(EditText)findViewById(R.id.data3id);
btn3=(Button)findViewById(R.id.updatebtnid);
//dba=new DBAdapter(Activity2.this);
final Intent intent = getIntent();
mRowId=intent.getStringExtra("KEYROWID");
btn3.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
dba = new DBAdapter(v.getContext());
data3=ed3.getText().toString();
updatequery = "UPDATE "+ DBAdapter.DATABASE_TABLE+ " SET ";
updatequery = updatequery + DBAdapter.KEY_DATA3+" = '" + data3 +"'";
Log.v("updatequery",updatequery);
updatequery = updatequery + " WHERE _id = '" + mRowId + "';";
Log.v("updatequery-new",updatequery);
Log.v("test-1","update");
try{
Toast.makeText(v.getContext(), "UPDATING", Toast.LENGTH_SHORT).show();
Log.v("test-2","update");
dba.open();
myDB.execSQL(updatequery);
}catch(NullPointerException e){
//e.getCause();
e.getMessage();
}finally{
Log.v("test-3","update");
if(myDB != null)myDB.close();
dba.close();
}
}
});
}
我正在使用的数据库:
public class DBAdapter {
public static final String KEY_ROWID= "_id";
public static final String KEY_DATA1="data1";
public static final String KEY_DATA2="data2";
public static final String KEY_DATA3="data3";
private static String TAG="DBAdapter";
static String DATABASE_NAME="update.db";
static final String DATABASE_TABLE="update_table";
private static int DATABASE_VERSION=2;
private final Context context;
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static final String DATABASE_CREATE =
"create table update_table(_id integer primary key autoincrement, "
+ "data1 varchar, data2 varchar, data3 varchar);";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL(DATABASE_CREATE);
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS update_table");
onCreate(db);
}
}
public DBAdapter(Context ctx) {
this.context = ctx;
open();
}
public DBAdapter open() throws SQLException {
mDbHelper = new DatabaseHelper(context);
mDb = mDbHelper.getWritableDatabase();
return this;
}
public void close() {
mDbHelper.close();
}
public long createddb(String data1,String data2,String data3) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_DATA1, data1);
initialValues.put(KEY_DATA2, data2);
initialValues.put(KEY_DATA3, data3);
return mDb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean deleteSaleseditdetails(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
public int fetchmaxId(){
String selectQuery = "SELECT max("+KEY_ROWID+") from " + DATABASE_TABLE ;
Cursor c= mDb.rawQuery(selectQuery, null);
c.moveToFirst();
return c.getInt(0);
}
public boolean deletedb(long rowId) {
return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}
}
以下是myLogcat文件:
02-04 11:53:20.216: E/AndroidRuntime(11235): FATAL EXCEPTION: main
02-04 11:53:20.216: E/AndroidRuntime(11235): java.lang.NullPointerException
02-04 11:53:20.216: E/AndroidRuntime(11235): at com.example.testupdate.Activity2$1.onClick(Activity2.java:59)
02-04 11:53:20.216: E/AndroidRuntime(11235): at android.view.View.performClick(View.java:2408)
02-04 11:53:20.216: E/AndroidRuntime(11235): at android.view.View$PerformClick.run(View.java:8816)
02-04 11:53:20.216: E/AndroidRuntime(11235): at android.os.Handler.handleCallback(Handler.java:587)
02-04 11:53:20.216: E/AndroidRuntime(11235): at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 11:53:20.216: E/AndroidRuntime(11235): at android.os.Looper.loop(Looper.java:123)
02-04 11:53:20.216: E/AndroidRuntime(11235): at android.app.ActivityThread.main(ActivityThread.java:4627)
02-04 11:53:20.216: E/AndroidRuntime(11235): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 11:53:20.216: E/AndroidRuntime(11235): at java.lang.reflect.Method.invoke(Method.java:521)
02-04 11:53:20.216: E/AndroidRuntime(11235): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
02-04 11:53:20.216: E/AndroidRuntime(11235): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
02-04 11:53:20.216: E/AndroidRuntime(11235): at dalvik.system.NativeStart.main(Native Method)