插入到 sqlite 数据库时出现空指针异常 - Android

发布于 2024-10-20 16:36:46 字数 1809 浏览 1 评论 0原文

因此,我有两个类,一类添加任务,一类添加类别。添加任务时,您可以将该任务与使用下拉列表填充的类别相关联,并且数据库通过使用触发器强制执行外键。我已经让应用程序正确添加任务,但我在添加类别类中捕获了空指针异常,并且我无法确定它在哪里 - 我已经查看了 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烛影斜 2024-10-27 16:36:46

我没有看到 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 at dbHelper.AddCategory(cat); and see if dbHelper is null.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文