我的应用程序想要计算一个月的总销售额。我用 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)