Android SQLite SUM, throws NullPointer

so I am working on an app, and I just started learning SQLite for android, apparently I am stuck for 3 days now on one problem. Whenever I try to run query to SUM Up fields from table where month is equal to specific month, I get NullPointer thrown.

package com.example.droid;
import java.text.SimpleDateFormat;
import java.util.Calendar;

import com.example.droid.SqDb;

import android.accounts.Account;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.text.format.DateFormat;
import android.util.Log;


public class DbCon {

    private static SQLiteDatabase database;
    private SqDb SqHelper;
    private String[] UserColumns = 
        {
            SqDb.ACCOUNT_ID,
            SqDb.ACCOUNT_NAME,
            SqDb.ACCOUNT_USERNAME,
            SqDb.ACCOUNT_PASSWORD,
            SqDb.ACCOUNT_AGE,
            SqDb.ACCOUNT_CITY,
            SqDb.ACCOUNT_COUNTRY
        };
    private String [] GuestColumns = 
        {
            SqDb.GUEST_ID,
            SqDb.GUEST_NAME
        };
    private String [] TransColumns =
        {
            SqDb.TRAN_ID,
            SqDb.TRAN_DESC,
            SqDb.TRAN_TYPE,
            SqDb.TRAN_TIME,
            SqDb.TRAN_DAY,
            SqDb.TRAN_MON,
            SqDb.TRAN_YEAR,
            SqDb.TRAN_CAT,
            SqDb.TRAN_VAL
        };

    private static DbCon myDCon;

    DbCon(Context context)
    {
        SqHelper = SqDb.getInstance(context);
    }

    public static DbCon getInstance(Context context)
    {
        if(myDCon == null)
            {
            myDCon = new DbCon(context.getApplicationContext());
            }
        return myDCon;
    }

    public void open() throws SQLException
    {
        database = SqHelper.getWritableDatabase();
    }

    public void close()
    {
        SqHelper.close();
    }

    public void createUser(String username, String password, String name, String Age, String City, String Country)
    {
        ContentValues values = new ContentValues();
        values.put(SqDb.ACCOUNT_USERNAME, username);
        values.put(SqDb.ACCOUNT_PASSWORD, password);
        values.put(SqDb.ACCOUNT_NAME, name);
        values.put(SqDb.ACCOUNT_AGE, Age);
        values.put(SqDb.ACCOUNT_CITY, City);
        values.put(SqDb.ACCOUNT_COUNTRY, Country);
        database.insert(SqDb.ACCOUNT, null, values);
    }

    public void createGuest(String name)
    {
        ContentValues values = new ContentValues();
        values.put(SqDb.GUEST_NAME, name);
        database.insert(SqDb.GUEST, null, values);
        }

    public void AddTask(String desc, String type, String cat, double Value)
    {
        ContentValues values = new ContentValues();
        values.put(SqDb.TRAN_DESC, desc);
        values.put(SqDb.TRAN_TYPE, type);
        values.put(SqDb.TRAN_CAT, cat);
        values.put(SqDb.TRAN_VAL, Value);
        SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
        Calendar cal = Calendar.getInstance();
        String timedata = df.format(cal.getTime());
        String[] whens = timedata.split(" ");
        String time = whens[1];
        String[] dates = whens[0].split("/");
        values.put(SqDb.TRAN_TIME, time);
        values.put(SqDb.TRAN_DAY, dates[2]);
        values.put(SqDb.TRAN_MON, dates[1]);
        values.put(SqDb.TRAN_YEAR, dates[0]);
        database.insert(SqDb.TRAN, null, values);
    }
    Calendar cal = Calendar.getInstance();
    public int getMonthInt(){
        int month = cal.get(Calendar.MONTH);
        return month;
    }
    public double CountMonthExp()
    {
        double din = 0;
        this.open();
        Cursor c = database.rawQuery("SELECT sum("+SqDb.TRAN_VAL+") as"+SqDb.TRAN_VAL+" FROM "+SqDb.TRAN+" WHERE "+SqDb.TRAN_TYPE+"=EXPENDITURE and "+SqDb.TRAN_MON+"=3", null);
        if(c.moveToFirst()){
            din = c.getDouble(0);
        }
        else{
            din = 0;
        }
        return din;
    }


}

Any suggestions? And yes the table is created well and working, I tested inserting records works fine.

Here is the error:

03-19 00:18:38.119: E/AndroidRuntime(19822): FATAL EXCEPTION: main 03-19 00:18:38.119: E/AndroidRuntime(19822): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.droid/com.example.droid.Hist_act}: android.database.sqlite.SQLiteException: no such column: EXPENDITURE: , while compiling: SELECT sum(tran_val) astran_val FROM tran WHERE tran_type=EXPENDITURE and tran_mon=3 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1651) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1667) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.app.ActivityThread.access$1500(ActivityThread.java:117) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:935) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.os.Handler.dispatchMessage(Handler.java:99) 03-19 00:18:38.119: E/Andro idRuntime(19822): at android.os.Looper.loop(Looper.java:130) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.app.ActivityThread.main(ActivityThread.java:3687) 03-19 00:18:38.119: E/AndroidRuntime(19822): at java.lang.reflect.Method.invokeNative(Native Method) 03-19 00:18:38.119: E/AndroidRuntime(19822): at java.lang.reflect.Method.invoke(Method.java:507) 03-19 00:18:38.119: E/AndroidRuntime(19822): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867) 03-19 00:18:38.119: E/AndroidRuntime(19822): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625) 03-19 00:18:38.119: E/AndroidRuntime(19822): at dalvik.system.NativeStart.main(Native Method) 03-19 00:18:38.119: E/AndroidRuntime(19822): Caused by: android.database.sqlite.SQLiteException: no such column: EXPENDITURE: , while compiling: SELECT sum(tran_val) astran_val FROM tran WHERE tran_type=EXPENDITURE and tran_mon=3 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQ LiteCompiledSql.native_compile(Native Method) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:92) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:65) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:83) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:49) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:42) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1356) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1324) 03-19 00:18:38.119: E/AndroidRuntime(19822): at com.example.droid.DbCon.CountMonthExp(DbCon.java:122) 0 3-19 00:18:38.119: E/AndroidRuntime(19822): at com.example.droid.Hist_act.onCreate(Hist_act.java:42) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 03-19 00:18:38.119: E/AndroidRuntime(19822): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1615) 03-19 00:18:38.119: E/AndroidRuntime(19822): ... 11 more


I'd say there are at least two issues here:

SELECT sum(tran_val) astran_val FROM tran WHERE tran_type=EXPENDITURE and tran_mon=3

I'd suspect you mean something like:

SELECT sum(tran_val) as tran_val FROM tran WHERE tran_type='EXPENDITURE' and tran_mon=3

I am sure you will be able to change the code yourself to achieve those adjustments.

链接地址: http://www.djcxy.com/p/96816.html

上一篇: 列出Android设备上的所有图像

下一篇: Android SQLite SUM引发NullPointer