2 i405817461 i405817461 于 2013.06.07 10:33 提问

在更新和删除SQLite数据库时报错NullPointerException

我的Activity首先打开了数据库。

private DBAdapter dbAdepter;

dbAdepter = new DBAdapter(this);
    dbAdepter.open();

然后更新操作如下:

public class Heimingdan_Alter extends Activity {

private DBAdapter dbAdepter;

private EditText editname;
private EditText edittelephone;
private Button update1;
private Button delete;

private String name,phone,id,name0,telephone0;
//public int id;
public void onCreate(Bundle savedInstanceState){
    super.onCreate(savedInstanceState);
     //设置无标题
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.fix_blacklist);

    dbAdepter = new DBAdapter(this);
    dbAdepter.open();


    //MyApplication.getInstance().addActivity(this);
    editname=(EditText)findViewById(R.id.hmd_name);
    edittelephone=(EditText)findViewById(R.id.hmd_num);
    update1=(Button)findViewById(R.id.ensure);
    delete=(Button)findViewById(R.id.delete);

    Intent i = getIntent();
    telephone0=i.getStringExtra("telephone");
    name0=i.getStringExtra("name");
    id=i.getStringExtra("id");

    editname.setText(i.getStringExtra("name"));
    edittelephone.setText(i.getStringExtra("telephone"));

    //更新事件
    update1.setOnClickListener(new OnClickListener() {
        public void onClick(View arg0) {
            name=editname.getText().toString();
            phone=edittelephone.getText().toString();
            if(name.equals("")||phone.equals("")){
                Toast.makeText(Heimingdan_Alter.this, "您输入的信息不完整哦~",Toast.LENGTH_LONG).show();
            }
            else {


                BlackNumber blacknumber = new BlackNumber();
                BlackNumber blacknumber2 = new BlackNumber();
                blacknumber.telephone = phone;
                blacknumber.name = name;
                Log.v("更新的黑名单值", blacknumber.telephone+blacknumber.name);

                blacknumber2.telephone = telephone0;
                blacknumber2.name = name0;
                Log.v("以前的黑名单值", blacknumber2.telephone+blacknumber2.name);


                    dbAdepter.updateblack(id,blacknumber,blacknumber2);




                onBackPressed();
                Toast.makeText(Heimingdan_Alter.this, "修改成功!", Toast.LENGTH_LONG).show();


            }
        }
    });
}

然后在DBAdapter.java中如下

public void open() throws SQLiteException {

    try {
        db = dbOpenHelper.getWritableDatabase();
        Log.v("DBAdapter_Open_getWritableDatabase",
                "DBAdapter_Open_getWritableDatabase");
    } catch (SQLiteException ex) {
        db = dbOpenHelper.getReadableDatabase();
        Log.v("DBAdapter_Open_getReadableDatabase",
                "DBAdapter_Open_getReadableDatabase");
    }
}
public int updateblack(String id,BlackNumber blacknumber, BlackNumber blacknumber2) {
    ContentValues newValues = new ContentValues();
    Log.e("updateblack",blacknumber.name + blacknumber.telephone+ "from"  + blacknumber2.telephone);
    newValues.put("name", blacknumber.name);
    newValues.put("telephone", blacknumber.telephone);

    int _id=Integer.parseInt(id);

    int colunm = db.update("blacklist", newValues, "_id" + "=" + _id , null);




    Log.e("updateblacksuccess", blacknumber.name + ":" + blacknumber.telephone
            + "from"  + blacknumber2.name);
    return colunm;

}

其中BlackNumber是一个含有两个String(name,telephone)和一个int(id)的类。

每次调试都说是
int colunm = db.update("blacklist", newValues, "_id" + "=" + _id , null);
这一行出错。
但是调试log信息表示id得到了也是对的。blacklist名字也没错。这一行我加到插入的方法里就好使,但是到这里就报错。是个什么问题?
求大神

1个回答

shaojie519
shaojie519   2013.06.07 14:19

输出下你的 db, blacknumber, blacknumber2 三个对象,检查下是否为空

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!