help me out to get the sum of a column in sqlitedatabase android to a string so that I can view it on a textview I'm a newbie at this. I have a logic to find the sum
this is my ACTIVITY.java
public class CollectionDetailsActivity extends AppCompatActivity {
TextView daily, monthly;
SqliteDatabase mydb;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_collection_details);
daily= findViewById(R.id.dayCollection);
monthly = findViewById(R.id.monthCollection);
SimpleDateFormat date = new SimpleDateFormat("dd/MM/yyyy"); String timeStamp = date.format(new Date()) ; Toast.makeText(getApplicationContext(), timeStamp, Toast.LENGTH_SHORT).show();
String data = " ";
Cursor dailyCollection = mydb.dailyCollection(timeStamp);
if (dailyCollection.moveToNext()) {
data = String.valueOf(dailyCollection.getColumnIndex("total"));
Toast.makeText(getApplicationContext(), data, Toast.LENGTH_SHORT).show(); }
else{ Toast.makeText(getApplicationContext(),"nothing here!! " , Toast.LENGTH_SHORT).show(); }
daily.setText(data);
}
But I can't get it to display on the textview. Somebody please help me.!
this is my Database function
public Cursor dailyCollection(String timeStamp) {
SQLiteDatabase db = this.getReadableDatabase();
String sql2 = "SELECT SUM ("+COLUMN_CM_C_AMOUNT+")AS total FROM "+TABLE_MONEY_COLLECTION+" WHERE "+COLUMN_CM_DATE + "=?";
String balance= "0";
Cursor cursor = null;
if (db != null) {
cursor = db.rawQuery(sql2, new String[]{timeStamp});
}else { Toast.makeText(context,"Not Availbel Yet",Toast.LENGTH_SHORT).show();
}
return cursor;
}
this what i am getting
E/com.ancorp.apl: Invalid ID 0x00000000.
D/AndroidRuntime: Shutting down VM
E/AndroidRuntime: FATAL EXCEPTION: main Process: com.ancorp.aplo, PID: 5641 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.ancorp.aplo/com.ancorp.aplo.activity.CollectionDetailsActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.ancorp.aplo.database.SqliteDatabase.dailyCollection(java.lang.String)' on a null object reference at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3488) at android.app.ActivityThread
User contributions licensed under CC BY-SA 3.0