我遇到了一个问题:我需要从数据库中删除数据,但每当我运行代码时,它总会抛出NullPointerException。
以下是代码:
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.del:
try {
String selectedSpinner2 = spinner_2.getSelectedItem().toString();
System.out.println(selectedSpinner2);
catlist.deleteCategory(selectedSpinner2);
Toast.makeText(this, "Task Deleted", Toast.LENGTH_SHORT).show();
return true;
}
catch (NullPointerException e){
e.printStackTrace();
}
case R.id.set_cat:
spinner_2.setOnItemSelectedListener(this);
}
return true;
}
对于数据库
public boolean deleteCategory(String category){
return DB.delete(TABLE_NAME, CAT_NAME + "=" + category, null) > 0;
}
堆栈跟踪:
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'boolean za.co.brdsa.kyle.planitto_doapp.CategoryList.deleteCategory(java.lang.String)' 在za.co.brdsa.kyle.planitto_doapp.Activity3.onMenuItemClick(Activity3.java:314)