Search code examples
androidcursornullpointerexception

NullPointer while using Cursor


I couldn't find a suitable title for my question. Here is the problem... After syso'ing the statement in getData(), it should print the values in the cursor, cMainTable, using the statement in displayDataForMainTable(). But, it doesn't. There are no exceptions, and errors. I have also kept the output that i get in the LogCat. Can someone help me with, why there is no output after the first print.

try {
    pm = new PortfolioManager(this);
    cMainTable = pm.getData("mainTable");
    if (cMainTable.equals(null)) {
        System.out.println("Null is returned");
    }
    displayDataForMainTable(cMainTable);
}
catch (Exception e) {
    System.out.println("problem at Oncreate Method");
}

getData() method:

public Cursor getData(String string) {
    System.out.println("Getting Data for Table " + string);
    c = sqlDB.rawQuery("select * from " + string, null);      
    return c;
}

displayDataForMainTable() method:

private void displayDataForMainTable(Cursor c2) {
    try {
        if (c2 != null) {
            if (c2.getCount() > 0) {
                c2.moveToFirst();
                do {
                    String SNo = c2.getString(c2.getColumnIndex("scriptnumber"));
                    String SName = c2.getString(c2.getColumnIndex("scriptname"));
                    int quant = Integer.parseInt(c2.getString(c2.getColumnIndex("totalquantity")));
                    double avg = Double.parseDouble(c2.getString(c2.getColumnIndex("averageprice")));
                    double cur = Double.parseDouble(c2.getString(c2.getColumnIndex("currentprice")));
                    double log = Double.parseDouble(c2.getString(c2.getColumnIndex("lossorgain")));
                    System.out.println("Inserted Values are ::: " + SNo
                            + " " + SName + " " + String.valueOf(quant)
                            + " " + String.valueOf(avg) + " "
                            + String.valueOf(cur) + " "
                            + String.valueOf(log));
                } while (c2.moveToNext());
            }
        } 
        else {
            System.out.println("Cursor c2 is Empty");
        }
    } catch (Exception e) {
        System.out.println("Exception in displayDataForMainTable");
    }
}

output

01-18 11:02:27.523: D/dalvikvm(468): GC_EXTERNAL_ALLOC freed 47K, 53% free 2567K/5379K, external 2090K/2137K, paused 45ms
01-18 11:02:33.983: I/System.out(468): Getting Data for Table mainTable
01-18 11:02:36.584: W/KeyCharacterMap(468): No keyboard for id 0
01-18 11:02:36.584: W/KeyCharacterMap(468): Using default keymap: /system/usr/keychars/qwerty.kcm.bin

Edit 1 ::: I have modified the getData() as suggested by Nasser, but the output remains the same..

public Cursor getData(String string) {
    System.out.println("Getting Data for Table "+string);
    String table = string;
    sqlDB = getReadableDatabase();
    c = sqlDB.query(table, null, null, null, null, null, null);
    //c = sqlDB.rawQuery("select * from "+string, null);        
    return c;
}

As I read, passing null to the columns(2nd argument) will return all the rows. If this is correct, then the result is the same.


Solution

  • It seems that Eclipse has shown mercy on me and is displaying the output. Fought for over 4 hours changing code, surfing, finally ended up with no changes in code.

    No matter how smart you are as a coder, IDEs always make you feel a novice.