开发者

Why does my Android app crash on Row insert?

I'm just getting started with android development, and I've ran into some problems my textbook isn't helping me with.

This is my insert function: public long insertEntry(BudgetsItem item){ assert item.id == -1;

    ContentValues newBudgetValues = new ContentValues();
    newBudgetValues.put("name", item.name);
    newBudgetValues.put("balance", item.balance);

    item.id = db.insert(DATABASE_TABLE_NAME, null, newB开发者_如何学GoudgetValues);

    return item.id;
}

This is my table definition:

private static final String TABLE_CREATE = "CREATE " + DATABASE_TABLE_NAME + "(\n" +
    "id INTEGER PRIMARY KEY AUTOINCREMENT, \n" +
    "name TEXT, \n" +
    "balance DOUBLE);";

And this is what is calling the adapter before it crashes:

        BudgetsAdapter adapter = new BudgetsAdapter(getBaseContext());
        BudgetsItem newBudget = new BudgetsItem(budgetName, balance);
        if(adapter.insertEntry(newBudget) != -1){
            Toast.makeText(getApplicationContext(), "Budget Created!", Toast.LENGTH_SHORT);
            finish();
        }else{
            Toast.makeText(getApplicationContext(), "Oops, something failed.", Toast.LENGTH_SHORT);
        }

It crashes on the db.insert line with a NullPointerException. Can anyone tell me why?


Your not giving us any information on you db object (I guess represents your database.

since you said it crashes on db insert I would have to guess that db is not initialized at the moment your doing an insert. I would suggest 1) adding a if(db != null)

if the last line in your crash log is effectively on the db.insert.

if there are more lines please add your stack to your post so we can help


In that case, there is just one thing that can be null: db. Are you sure you are initializing them?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜