[英]Null Pointer Exception when inserting to sqlite database - Android
因此,我有兩個類,一個添加任務,一個添加類別。 添加任務時,可以將該任務與使用下拉列表填充的類別關聯,並且數據庫通過使用觸發器來強制執行外鍵。 我已經使應用程序正確地添加了任務,但是我的添加類別類捕獲了空指針異常,並且我難以確定它的位置-我在logcat中進行了查看,但是它並不是特別有用。
該類如下:
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();
}
}
如果您需要更多代碼,請詢問。 任何幫助深表感謝。 總是有助於讓更多的人關注代碼以發現錯誤:)。
我看不到dbHelper
的初始化位置,因此我懷疑它會返回null。 在dbHelper.AddCategory(cat);
處放置一個斷點dbHelper.AddCategory(cat);
並查看dbHelper
是否為null。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.