Android: how to sum real values from Sqlite?

I've got the following code to get a sum of real values (debt amounts stored in an SQlite DB: public double getsumdebt() { Cursor mCursor = db.rawQuery("SELECT SUM(CurrentAmount) FROM tblDebts", null); if(mCursor.moveToFirst()) { return mCursor.getDouble(0); } return mCursor.getDouble(0); } I am trying to return the sum of all the values in the "CurrentAmount" column and display it in a textview: DbAdapter db2 = new DbAdapter(this); double sum= new Double(db2.getsumdebt()); //sum = db2.getsumdebt(); bar.setText("Progress: "+currencysymbol +current +" / " + currencysymbol + sum); however, when I run the app in the emulator, I get a Force close during launch, with the following in the DDMS log: > 09-26 19:01:19.098: ERROR/AndroidRuntime(677): FATAL EXCEPTION: main 09-26 19:01:19.098: ERROR/AndroidRuntime(677): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.evanrich.android.debtdestroyer/com.evanrich.android.debtdestroyer.mainActivity}: java.lang.NullPointerException 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.os.Handler.dispatchMessage(Handler.java:99) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.os.Looper.loop(Looper.java:123) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.app.ActivityThread.main(ActivityThread.java:4627) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at java.lang.reflect.Method.invokeNative(Native Method) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at java.lang.reflect.Method.invoke(Method.java:521) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at dalvik.system.NativeStart.main(Native Method) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): Caused by: java.lang.NullPointerException 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at com.evanrich.android.debtdestroyer.database.DbAdapter.getsumdebt(DbAdapter.java:106) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at com.evanrich.android.debtdestroyer.mainActivity.onCreate(mainActivity.java:73) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 09-26 19:01:19.098: ERROR/AndroidRuntime(677): ... 11 more It looks like it's not able to sum the real values stored in the DB. Can anyone help me? FYI, this is my table constructor: private static final String DATABASE_CREATE = "CREATE TABLE IF NOT EXISTS tblDebts (_id integer primary key autoincrement, " + "Debt text not null, StartingAmount real, CurrentAmount real, " + "InterestRate real, DueDate integer, MinimumPayment real );"; Thanks.

以上就是Android: how to sum real values from Sqlite?的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏