I have a problem: I need to delete data from a database, but whenever I run the code, it always throws a NullPointerException.
Here is the code:
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;
}
And for the database
public boolean deleteCategory(String category){
return DB.delete(TABLE_NAME, CAT_NAME + "=" + category, null) > 0;
}
And the stacktrace:
java.lang.NullPointerException: Attempt to invoke virtual method 'boolean za.co.brdsa.kyle.planitto_doapp.CategoryList.deleteCategory(java.lang.String)' on a null object reference at za.co.brdsa.kyle.planitto_doapp.Activity3.onMenuItemClick(Activity3.java:314)