0

I am having some issues with the Zxing android scanner and cursors returning results from my database.

What I want to happen is the scanner will return the result from the scan, this string will then be used in a query in the database and the results returned into a cursor that will be used for an insert into another table.

The error I am getting is a Null point error, I know what a null pointer error is but I am struggling to find where I have gone wrong in the code.

Query

public Cursor getBarcodeInfo(String number){
    return db.rawQuery("select _id, barcode_item_name, barcode_measurement, barcode_unit from barcode where barcode_number = " + number, null);
}

Cursor

               String barcode_number = intent.getStringExtra("SCAN_RESULT");

            //Code to add scanned item to DB, needs barcode data before can be ruin
            final Cursor barInformation = adapter.getBarcodeInfo(barcode_number);

            barInformation.moveToFirst();
            String itemName = barInformation.getString(barInformation.getColumnIndex("barcode_item_name"));

            Toast toast = Toast.makeText(this, "Content:" + itemName ,Toast.LENGTH_SHORT);
            toast.show();

Error

02-11 20:28:07.966 18691-18691/com.example.rory.prototypev2 E/AndroidRuntime:  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor android.database.sqlite.SQLiteDatabase.rawQuery(java.lang.String, java.lang.String[])' on a null object reference
02-11 20:28:07.966 18691-18691/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.DBMain.getBarcodeInfo(DBMain.java:437)
02-11 20:28:07.966 18691-18691/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.scanner.onActivityResult(scanner.java:73)
kandroidj
  • 13,784
  • 5
  • 64
  • 76
JJSmith
  • 1,833
  • 4
  • 17
  • 26

1 Answers1

0

try:

public Cursor getBarcodeInfo(String number){

    // Safe check to make sure db and number is not null
    if(db == null || number == null){

         return null;
    }

    return db.rawQuery("select _id, barcode_item_name, barcode_measurement, barcode_unit from barcode where barcode_number = ?", new String[]{number});
}
kandroidj
  • 13,784
  • 5
  • 64
  • 76
  • That seems to have fixed it thanks, just pointed out that the query isnt actually returning anything so that is were the error is coming from – JJSmith Feb 11 '16 at 21:09