database returns a null, despite there being records in the database
private static final String DATABASE_NAME="PlayGamesdb";
private static final String DATABASE_TABLE="peopleTable";
public int fetchPlacesCount() {
int count = 0;
//String[] columns = new String[] {KEY_ROWID, KEY_NAME, KEY_HITS };
Cursor c = ourDatabase.rawQuery("SELECT COUNT(*) from DATABASE_TABLE", null);
c.moveToFirst();
count = c.getInt(0);
return count;
}
any ideas on what is wrong?
01-24 19:30:23.050: E/AndroidRuntime(1761): FATAL EXCEPTION: main
01-24 19:30:23.050: E/AndroidRuntime(1761): java.lang.NullPointerException
01-24 19:30:23.050: E/AndroidRuntime(1761): at com.example.dbsample.PlayGame.fetchPlacesCount(PlayGame.java:93)
01-24 19:30:23.050: E/AndroidRuntime(1761): at com.example.dbsample.SQLiteExample.onClick(SQLiteExample.java:123)
01-24 19:30:23.050: E/AndroidRuntime(1761): at android.view.View.performClick(View.java:3511)
here is the onclick that in the other class used to call the fetchPlacesCount method
case R.id.bSQLnumberofrows:
int x = 0;
PlayGame ec = new PlayGame(this);
ec.open();
x = ec.fetchPlacesCount();
ec.close();
sqlRow.setText(x);
break;
EDIT:
after fixing the problems the code below is now working and shows the correct number of rows as a Toast message, so now one problem fixed and one more to go, still error with the setText method for putting the number in the EditText. that is what I will work on next, to see what is wrong with this textView.
using the Toast message was a good way to find out that the method was returning the correct number.
public int fetchPlacesCount() {
int count = 0;
Cursor q = ourDatabase.rawQuery("SELECT COUNT(*) from " + DATABASE_TABLE, null);
q.moveToFirst();
count = q.getInt(0);
return count;
}
int the other class
case R.id.bSQLnumberofrows:
PlayGame ec = new PlayGame(this);
ec.open();
int x = ec.fetchPlacesCount();
ec.close();
// sqlRow.setText(x);
Toast.makeText(SQLiteExample.this, "value of x " + x , Toast.LENGTH_SHORT).show();
break;
I think you should return the
contvariable instead ofcountas you are not assigning any value tocountvariable.EDITED:
Fire your query as below: