Android SQLite 空指针异常 [英] Android SQLite Null Pointer Exception

查看:22
本文介绍了Android SQLite 空指针异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 LogCat 中得到了这个:

I am getting this in LogCat:

05-20 17:16:34.721: E/AndroidRuntime(30461): FATAL EXCEPTION: main
05-20 17:16:34.721: E/AndroidRuntime(30461): java.lang.NullPointerException
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.database.sqlite.SQLiteStatement.releaseAndUnlock(SQLiteStatement.java:290)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.database.sqlite.SQLiteStatement.executeUpdateDelete(SQLiteStatement.java:96)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.database.sqlite.SQLiteDatabase.updateWithOnConflict(SQLiteDatabase.java:1810)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.database.sqlite.SQLiteDatabase.update(SQLiteDatabase.java:1761)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at com.kickinglettuce.debtplannerpro.DebtDataSource.updateDebt(DebtDataSource.java:130)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at com.kickinglettuce.debtplannerpro.manageDebts$4.onClick(manageDebts.java:184)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.view.View.performClick(View.java:3511)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.view.View$PerformClick.run(View.java:14105)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.os.Handler.handleCallback(Handler.java:605)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.os.Looper.loop(Looper.java:137)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at android.app.ActivityThread.main(ActivityThread.java:4447)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at java.lang.reflect.Method.invokeNative(Native Method)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at java.lang.reflect.Method.invoke(Method.java:511)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-20 17:16:34.721: E/AndroidRuntime(30461):    at dalvik.system.NativeStart.main(Native Method)

这是与之相关的代码:

protected void onListItemClick(ListView l, View v, int position, long id) {

    List<Debt> values = datasource.getAllDebt();
            datasource.open();

    Debt item = values.get(position);
    final long boxId = item.getId();
    // final String BoxId = String.valueOf(boxId);
    final String BoxName = item.getName();
    final String BoxBalance = item.getBalance();
    final String BoxApr = item.getApr();
    final String BoxPayment = item.getPayment();

    // set up dialog
    final Dialog dialog = new Dialog(manageDebts.this);
    dialog.setContentView(R.layout.custom_dialog);
    dialog.setTitle("Edit Debt Details");
    dialog.setCancelable(true);

    // set up text
    TextView tv1 = (TextView) dialog.findViewById(R.id.textView1);
    TextView tv2 = (TextView) dialog.findViewById(R.id.textView2);
    TextView tv3 = (TextView) dialog.findViewById(R.id.textView3);
    TextView tv4 = (TextView) dialog.findViewById(R.id.textView4);
    EditText et1 = (EditText) dialog.findViewById(R.id.editText1);
    EditText et2 = (EditText) dialog.findViewById(R.id.editText2);
    EditText et3 = (EditText) dialog.findViewById(R.id.editText3);
    EditText et4 = (EditText) dialog.findViewById(R.id.editText4);

    tv1.setText("Debt Description");
    tv2.setText("Balance");
    tv3.setText("APR");
    tv4.setText("Monthly Payment");

    et1.setText(BoxName);
    et2.setText(BoxBalance);
    et3.setText(BoxApr);
    et4.setText(BoxPayment);

    // set up button
    Button button = (Button) dialog.findViewById(R.id.button1);
    button.setOnClickListener(new OnClickListener() {
        public void onClick(View v) {


            datasource.updateDebt(Long.valueOf(boxId), BoxName, BoxBalance, BoxApr,
                    BoxPayment);
            dialog.dismiss();

        }
    });

    datasource.close();

    dialog.show();
}

以及我的数据库类中的更新方法:

And the Update Method in my database class:

public boolean updateDebt(long updateId, String debtName, String debtTotal,
        String debtApr, String paymentGoal) {

     ContentValues values = new ContentValues();
     values.put(MySQLiteHelper.COLUMN_DEBT_NAME, debtName);
     values.put(MySQLiteHelper.COLUMN_DEBT_TOTAL, debtTotal);
     values.put(MySQLiteHelper.COLUMN_APR, debtApr);
     values.put(MySQLiteHelper.COLUMN_PAYMENT, paymentGoal);
     String whereClause = MySQLiteHelper.COLUMN_ID + " = ?";
     String[] whereArgs = new String[]{ String.valueOf(updateId) };
     return database.update(MySQLiteHelper.TABLE_DEBT,
             values, whereClause, whereArgs) > 0;
}

有什么建议吗?

推荐答案

看起来您在数据库关闭时尝试访问它.也许将 datasource.open() 放在 onCreate() 的开头,将 datasource.close() 放在 onCreate() 的末尾,并在您的班级中分别调用它们一次就可以解决您的问题.

Looks like your trying to access the db when it's been closed. Perhaps placing datasource.open() at the beginning of onCreate and datasource.close() at the end of onCreate() and calling them each just once in your class would solve your problem.

如果您在需要多次调用数据库的活动中编辑、创建和删除项目,请考虑在访问数据库的方法的开头调用 datasource.open(),然后在该方法的结尾调用 close()方法.

If you are editing, creating and deleting items in your activity requiring multiple calls to your database, consider calling datasource.open() at the beginning of a method that accesses the database and then close() at the end of of that method.

这篇关于Android SQLite 空指针异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆