开发者社区> 问答> 正文

在更新和删除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名字也没错。这一行我加到插入的方法里就好使,但是到这里就报错。是个什么问题?
求大神

展开
收起
a123456678 2016-07-18 14:19:21 2736 0
1 条回答
写回答
取消 提交回答
  • 输出下你的 db, blacknumber, blacknumber2 三个对象,检查下是否为空

    2019-07-17 19:57:50
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
2022 DTCC-阿里云一站式数据库上云最佳实践 立即下载
云时代的数据库技术趋势 立即下载
超大型金融机构国产数据库全面迁移成功实践 立即下载