0

私のアプリケーションは、1 か月の総売上を計算したいと考えています。SQLステートメントの合計を使用しました。しかし、アプリケーションを実行すると失敗します。なぜこのエラーが発生するのかわかりません。人々がこれを修正するのを手伝ってくれることを期待してください。とても感謝しています。コード DB_Adapter

public class DB_Adapter {
public static final String DATE="date";
public static final String COST = "cost";
public static final String SALES = "sales";
public static final String NUMBER = "number";
public static final String PROFIT = "profit";
public static final String DB_NAME = "SAIGONFLEAMARKET";
public static final String DB_TABLE = "shipment";
public static final String MONTH="month";
public static final String YEAR="year";
public static final int DB_VERSION = 2;
private final Context mContext;
private SQLiteDatabase mDB;
private DBHelper mDBHelper;

public DB_Adapter(Context c) {
    mContext = c;
}

private static class DBHelper extends SQLiteOpenHelper
{

    public DBHelper(Context context, String name, CursorFactory factory,
            int version) {
        super(context, name, factory, version);
        // TODO Auto-generated constructor stub
    }

    // Khởi tạo một bảng trong database mang tên Saigonfleamarket
    @Override
    public void onCreate(SQLiteDatabase db) {
        // TODO Auto-generated method stub
        try
        {
            db.execSQL("CREATE TABLE shipment (number integer PRIMARY KEY autoincrement, date text, cost float, sales float, profit float, month text, year text );");
        } catch (SQLException ex)
        {
            ex.printStackTrace();
        }
    }

    // Tạo một database mới
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        Log.i("DBAdapter", "Updating database...");
        db.execSQL("DROP TABLE IF EXISTS shipment");
        onCreate(db);
    }
}

// Mở một kết nối đến database
public DB_Adapter openDB()
{
    mDBHelper = new DBHelper(mContext, DB_NAME, null, DB_VERSION);
    mDB = mDBHelper.getWritableDatabase();
    return this;
}

// sau khi hoàn thành, đóng database lại
public void closeDB()
{
    mDBHelper.close();
}

public Cursor Sum(String _month, String _year)

{
    String sql="select Sum (profit) from shipment where month ='"+_month+"' AND year='"+ _year+"'";
    Cursor mCursor = mDB.rawQuery(sql, null);
    int test = mCursor.getCount();
    if (test != 0)
    {
        mCursor.moveToFirst();
    }

    return mCursor;
}  
}

ログ猫のエラー

01-15 13:49:48.840: E/AndroidRuntime(6953): FATAL EXCEPTION: main
01-15 13:49:48.840: E/AndroidRuntime(6953): java.lang.NullPointerException
01-15 13:49:48.840: E/AndroidRuntime(6953):     at Data.DB_Adapter.Sum(DB_Adapter.java:116)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at com.example.xitinshop.Thongtin.onClick(Thongtin.java:40)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at android.view.View.performClick(View.java:2485)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at android.view.View$PerformClick.run(View.java:9080)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at android.os.Handler.handleCallback(Handler.java:587)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at android.os.Handler.dispatchMessage(Handler.java:92)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at android.os.Looper.loop(Looper.java:130)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at android.app.ActivityThread.main(ActivityThread.java:3683)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at java.lang.reflect.Method.invokeNative(Native Method)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at java.lang.reflect.Method.invoke(Method.java:507)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
01-15 13:49:48.840: E/AndroidRuntime(6953):     at dalvik.system.NativeStart.main(Native Method)
4

1 に答える 1

0

古いメソッドの代わりにこのメソッドを使用してください。

public Cursor Sum(String _month, String _year)

{
    String sql="select Sum(profit) from shipment where month =? AND year=?";
    Cursor mCursor = mDB.rawQuery(sql, new String[] { _month ,_year});
    int test = mCursor.getCount();
    if (test != 0)
    {
        mCursor.moveToFirst();
    }

    return mCursor;
}  
于 2013-01-15T07:21:48.997 に答える