简体   繁体   中英

retrieving data from sqlite: NullPointerException

I want to retrieve data from database and display it on LOG, here my code:

 String dd = "1/23/2013";

  String sp = "Sky Storm";

 Log.d("Checking",db.getMeterNUmber2(dd,sp).toString());

And getMeterNUmber2 are in DBAdapter and the data that im looking for are in database, so I just want to retrieve it.

My DB Adapter :

public ArrayList<String> getMeterNUmber2(String date , String Vname) throws SQLException {    

    String[] whereArgs = new String[]{date,Vname}; 

    Cursor mcursor = db.rawQuery("SELECT MeterNumber FROM " + SCAN_TABLE + " WHERE  Date = ? AND VendorName = ? ",whereArgs);

    ArrayList<String> results = new ArrayList<String>();
    while (mcursor.moveToNext()) {
        results.add(mcursor.getString(0));
    }

    return results;

}

I'm getting error in logcat:

01-24 19:57:13.590: E/AndroidRuntime(11662): FATAL EXCEPTION: main
01-24 19:57:13.590: E/AndroidRuntime(11662): java.lang.NullPointerException
01-24 19:57:13.590: E/AndroidRuntime(11662):    at scann.barcode.scan.DBAdapter.getMeterNUmber2(DBAdapter.java:303)
01-24 19:57:13.590: E/AndroidRuntime(11662):    at scann.barcode.scan.Result$1.onClick(Result.java:104)

i had the same problem as you and when i look at your log cat

 java.lang.NullPointerException ,

It returning null value , so that means It does not read into your Database,

please check if you have opened your database eg : db.open() and close

I think you can't just log in logcat ArrayList<String> returned from getMeterNUmber2 because it is a list. You need to retrieve single string values from there first: Best way to convert an ArrayList to a string .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM