-2

My dbhelper.java have method

public Cursor report(){
    SQLiteDatabase db = this.getReadableDatabase();
    Cursor c = db.rawQuery("SELECT * FROM Employees", null);
    return c;
}

Mainactivity.java

private List<Front1> viewReport(){

    List<Front1> employeeList1 = new ArrayList<>();

    Cursor c = db.report();

if (c != null && c.getCount() > 0) {         // Add the addition condition
if (c.moveToFirst()) {
    do{
        String ids=c.getString(c.getColumnIndex("e_ids"));
        String name=c.getString(c.getColumnIndex("e_name"));


        Front1 front1=new Front1(ids,name1);
       employeeList1.add(front1);

    }while (c.moveToNext());}


}else{ 

Front1 front1=new Front1("101","suran");
       employeeList1.add(front1);

 }

I have employee table.first time application install my table has empty so application crash.if i add dumny data stop application crash but user delete dummy data again application crash.how to solve null object reference initial stage .it mean select statement used if resultset empty .i want my application run not crash.any guidence will helpfull to me.

2 Answers2

0

As Cursor doesn't return null if the table is empty, you should use getCount() function to solve your problem. Your code should look like below:

if (c != null && c.getCount() > 0) {         // Add the addition condition
    if (c.moveToFirst()) {
        do{
            String ids=c.getString(c.getColumnIndex("e_ids"));
            String name=c.getString(c.getColumnIndex("e_name"));


            Front1 front1=new Front1(ids,name1);
           employeeList1.add(front1);

        }while (c.moveToNext());}


}
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34
  • Yes .size is zero .because first time install app that time table empty.sorry to ask clearly.i try if table empty skip and move else condition.(change your code add else conditon add array datas manually )but show the error Cursor c = db.report(); line. – suran kumar Dec 03 '17 at 16:21
  • If table is empty c will not return null. You have to check out if `getCount()` value is greater than 0. Please follow the procedure above. – tahsinRupam Dec 03 '17 at 17:55
  • I tried sir same error happen.code doesn't read next line.(because rows == 0).so code doesn't skip the if condition.Cursor c = db.report();struck this line. – suran kumar Dec 03 '17 at 18:22
  • Would you post the whole code and error log for the convenience? – tahsinRupam Dec 04 '17 at 04:34
  • It works .thank you – suran kumar Dec 04 '17 at 10:15
  • You're welcome. You may upvote the answer if helped. – tahsinRupam Dec 04 '17 at 10:18
0

Your issue is that a null Cursor will not be returned from a query. Rather in the situation of no data being extracted the Cursor will be empty.

This could be checked using the Cursor getCount() method. However, it is just as easily be checked by checking the return value (true or false) of a move???? method such as moveToNext. MoveToNext can also be used to traverse numerous rows in a while loop. As such, perhaps the simplest solution is to use :-

if (c.moveToNext()) {
    String ids=c.getString(c.getColumnIndex("e_ids"));
    String name=c.getString(c.getColumnIndex("e_name"));
    Front1 front1=new Front1(ids,name1);
    employeeList1.add(front1);
}

Obviously you may need to check the size of the returned List, as it's size could be 0.

MikeT
  • 51,415
  • 16
  • 49
  • 68
  • Yes .size is zero .because first time install app that time table empty.sorry to ask clearly.i try if table empty skip and move else condition.but show the error Cursor c = db.report(); line. – suran kumar Dec 03 '17 at 16:19
  • I want to get c== null.it mean return null from dbhelper – suran kumar Dec 03 '17 at 17:09
  • if table empty then return c .what will return c means null or int 0.how to set condition if (c== null) then ... else ... or any other. – suran kumar Dec 03 '17 at 17:22
  • You have errors or issues in code that isn't shown, probably because table Employees doesn't exist, which would possibly be an issue with code in the dbhelper's onCreate method. – MikeT Dec 03 '17 at 18:57
  • Use true or false(getcount()) in dbhelper solve my issue .thank you – suran kumar Dec 04 '17 at 10:15