So, I have two classes, one that adds tasks and one that adds categories. When adding a task you can associate that task with a category which is populated using a dropdown and the database enforces foreign keys by using triggers. I have got the application adding tasks correctly but I’m catching a null pointer exception with my add category class, and I’m having trouble pinning down where it is – I’ve looked in logcat but it hasn’t been particularly helpful.
The class is as follows:
public class AddCategory extends Activity {
EditText txtCatName;
DatabaseHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addcategory);
txtCatName = (EditText) findViewById(R.id.txtCatName);
}
public void btnAddCat_Click(View view) {
boolean ok = true;
try {
String name = txtCatName.getText().toString();
DatabaseMethods cat = new DatabaseMethods(name);
dbHelper.AddCategory(cat);
} catch (Exception ex) {
ok = false;
CatchError(ex.toString());
} finally {
if (ok) {
// NotifyEmpAdded();
Toasts.ShowTaskAddedAlert(this);
}
}
}
void CatchError(String Exception) {
Dialog diag = new Dialog(this);
diag.setTitle("Add new Category");
TextView txt = new TextView(this);
txt.setText(Exception);
diag.setContentView(txt);
diag.show();
}
void NotifyCatAdded() {
Dialog diag = new Dialog(this);
diag.setTitle(this.getString(R.string.add_category));
TextView txt = new TextView(this);
txt.setText(this.getString(R.string.category_added));
diag.setContentView(txt);
diag.show();
try {
diag.wait(1000);
} catch (InterruptedException e) {
CatchError(e.toString());
}
diag.notify();
diag.dismiss();
}
}
If you need any more code then please just ask. Any help is much appreciated. Always helps to get a couple of more pairs of eyes on code to find errors :).
I don’t see where
dbHelperis being initialized, so I would suspect it would be returning as null. Put a breakpoint atdbHelper.AddCategory(cat);and see ifdbHelperis null.