'Trying to use cursor.getCount(), cannot resolve symbol getCount()

i am making a android project when i try to use cursor.getCount(); it giving me error:

cannot resolve symbol getCount();

Here is my code

class GetNotesFromDbTask extends AsyncTask<Void, Void, Boolean> {

@Override
protected Boolean doInBackground(Void... params) {
    return null;
}

@Override
protected void onPreExecute() {
    ProgressDialog dialog = new ProgressDialog(NoteDetail.this);
    dialog.setTitle("Loading");
    dialog.setMessage("Please wait..");
    dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    dialog.setCancelable(false);
    dialog.show();
}


SQLiteDatabase db = helpers.getReadableDatabase();
String[] projection = {
        NoteContract.FeedEntry._ID,
        NoteContract.FeedEntry.NOTES_TITLE,
        NoteContract.FeedEntry.NOTES_ID,
        NoteContract.FeedEntry.NOTES_BODY,

};

String sortOrder =
        NoteContract.FeedEntry.NOTES_ID + " DESC";

Cursor cursor = db.query(
        NoteContract.FeedEntry.TABLE_NAME,  // The table to query
        projection,                               // The columns to return
        null,                                // The columns for the WHERE clause
        null,                            // The values for the WHERE clause
        null,                                     // don't group the rows
        null,                                     // don't filter by row groups
        sortOrder                                 // The sort order
);


if(cursor.getCount()>0) //here i am getting error

{

}

I define cursor too, i did not understand why it is not working.

Also,

i tried if(cursor !=null)

it is saying to me

cursor unknown class



Solution 1:[1]

Your code should be inside a method that is inside a class, not on the class level itself.

Previous lines are syntactically valid since they are variable declarations and initialisations. An if conditional is not valid on class level.

Solution 2:[2]

            class GetNotesFromDbTask extends AsyncTask<Void, Void, Boolean> {

            @Override
            protected Boolean doInBackground(Void... params) {
      SQLiteDatabase db = helpers.getReadableDatabase();
            String[] projection = {
                    NoteContract.FeedEntry._ID,
                    NoteContract.FeedEntry.NOTES_TITLE,
                    NoteContract.FeedEntry.NOTES_ID,
                    NoteContract.FeedEntry.NOTES_BODY,

            };

            String sortOrder =
                    NoteContract.FeedEntry.NOTES_ID + " DESC";

            Cursor cursor = db.query(
                    NoteContract.FeedEntry.TABLE_NAME,  // The table to query
                    projection,                               // The columns to return
                    null,                                // The columns for the WHERE clause
                    null,                            // The values for the WHERE clause
                    null,                                     // don't group the rows
                    null,                                     // don't filter by row groups
                    sortOrder                                 // The sort order
            );


            if(cursor.getCount()>0) //here i am getting error

            {

            }
                return null;
            }

            @Override
            protected void onPreExecute() {
                ProgressDialog dialog = new ProgressDialog(NoteDetail.this);
                dialog.setTitle("Loading");
                dialog.setMessage("Please wait..");
                dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
                dialog.setCancelable(false);
                dialog.show();
            }

            }  // this line add asynctask class end 

          protected void onCreate(Bundle savedInstanceState) {

            GetNotesFromDbTask  gnfd=new GetNotesFromDbTask ();
            //asynctask run onpre ->onbackground->onpost
gnfd.execute();


        }

Solution 3:[3]

I've been looking into this problem.

The test should be if(!cursor.moveToFirst())

with invalid cursor, this will return true.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 laalto
Solution 2
Solution 3 user18131432