插入到 sqlite 数据库时出现空指针异常 - 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();
}
}
如果您需要更多代码,请询问。非常感谢任何帮助。多多关注代码总是有助于发现错误:)。
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 :).
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我没有看到 dbHelper 在哪里被初始化,所以我怀疑它会返回 null。在
dbHelper.AddCategory(cat);
处放置断点,然后查看dbHelper
是否为 null。I don't see where
dbHelper
is being initialized, so I would suspect it would be returning as null. Put a breakpoint atdbHelper.AddCategory(cat);
and see ifdbHelper
is null.