0

i'm newbie at android programming, so please help me: I need to get the count of specific record, this my code:

public int countFiltered(String selection){
    Cursor cursor = database.query(DenverDBOpenHelper.TABLE_DENVERS, denverAllColumns, selection, null, null, null, null);
    Log.i(LOGTAG, "Get " + cursor.getCount() + " rows");

    int countItem = cursor.getCount();
    if(cursor==null) {
        return 0;
    }else
        return countItem;
}

and i call it on the other activity:

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_baby, container, false);

    txtPass = (TextView)rootView.findViewById(R.id.txtPass);
    int countPass = dataSource.countFiltered("score == 1");
    txtPass.setText(countPass);}

countPass show NullPinterException

and my score column type is INTEGER, this my DBOpenHelper:

public class DenverDBOpenHelper extends SQLiteOpenHelper {

private static final String LOGTAG = "LitleDenver";

private static final String DATABASE_NAME = "db_denver";
private static final int DATABASE_VERSION = 1;

public static final String TABLE_DENVERS = "denvers";
public static final String COLUMN_ID = "itemId";
public static final String COLUMN_CATEGORY = "category";
public static final String COLUMN_TITLE = "title";
public static final String COLUMN_DESC = "description";
public static final String COLUMN_DETAIL = "detail";
public static final String COLUMN_IMAGE = "image";
public static final String COLUMN_MIN = "min";
public static final String COLUMN_MAX = "max";
public static final String COLUMN_SCORE = "score";
public static final String COLUMN_STATUS = "status";

private static final String TABLE_CREATE =
        "CREATE TABLE " + TABLE_DENVERS + " (" +
                COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                COLUMN_CATEGORY + " TEXT, " +
                COLUMN_TITLE + " TEXT, " +
                COLUMN_DESC + " TEXT, " +
                COLUMN_DETAIL + " TEXT, " +
                COLUMN_IMAGE + " TEXT, " +
                COLUMN_MIN + " INTEGER, " +
                COLUMN_MAX + " INTEGER, " +
                COLUMN_SCORE + " INTEGER, " +
                COLUMN_STATUS + " INTEGER " +
                ")";
embo
  • 1
  • 2
  • possible duplicate of [How to trace a NullPointerException in a chain of getters](http://stackoverflow.com/questions/410890/how-to-trace-a-nullpointerexception-in-a-chain-of-getters) –  Jul 23 '15 at 21:14
  • NPE is something you can easily track. Say, if your cursor is null after query, then you need to check DB. There might be something wrong with how you query data. –  Jul 23 '15 at 21:15

3 Answers3

1

You will want to use the query as follow where the selectionArgs (which you have as null) as a String array of the value(s) you are looking for (think where clause). Also I cleaned up the code to not cause NPEs in the case that the cursor is null by calling getCount() on a null cursor

public int countFiltered(String selection, String argValue){
    String selectionArg = new String[] {  argValue };                                     
    Cursor cursor = database.query(DenverDBOpenHelper.TABLE_DENVERS,   
                                   denverAllColumns, 
                                   selection, 
                                   selectionArg, 
                                   null, null, null);
    if(cursor==null) {
        return 0;
    } else {
        return cursor.getCount();
    }
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View rootView = inflater.inflate(R.layout.fragment_baby, container, false);

    txtPass = (TextView)rootView.findViewById(R.id.txtPass);
    int countPass = dataSource.countFiltered("score = ?", "1");
    Log.i(LOGTAG, "Get countPass = " + countPass);
    txtPass.setText(countPass);
}
petey
  • 16,914
  • 6
  • 65
  • 97
0

Try modifying the second to last line to read like:

int countPass = dataSource.countFiltered("['score'] = 1");

I had a very similar problem and this was my fix.

Tyler Brown
  • 135
  • 2
0

you need to publish more information as the name of the fields, the visibility of the classes, which returns your logcat.

gfirem
  • 328
  • 3
  • 8