0

I am having some issues with ZXing's barcode scanner in my android application. I asked a question here yesterday that explains a null pointer error that I was having. The issue was solved and just showed that the cursor wasnt retrieving any information back.

My issues is that I know query is correct and I know the code is correct because I have tested it out without the scanner separately and it all works fine. Is it possible that I am calling to the database wrong in the barcode scanner intent or is there a certain way that I should be doing it?

Any help or guidance would be great!

onActivity Result

public void onActivityResult(int requestCode, int resultCode, Intent intent) {
    if (requestCode == 0) {
        if (resultCode == RESULT_OK) {
            //get the extras that are returned from the intent
            barcode_number = intent.getStringExtra("SCAN_RESULT");
            String bNumber = barcode_number.toString();

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

            barInformation.moveToFirst();
            String itemName = barInformation.getString(barInformation.getColumnIndex("barcode_item_name"));
            Toast toast = Toast.makeText(this, "Barcode Number:" + itemName ,Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}

Query

public Cursor barcodeInfo(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});
}

Error

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-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.DBMain.barcodeInfo(DBMain.java:437)
02-12 11:15:07.544 12808-12808/com.example.rory.prototypev2 E/AndroidRuntime:     at com.example.rory.prototypev2.scanner.onActivityResult(scanner.java:77)
Community
  • 1
  • 1
JJSmith
  • 1,833
  • 4
  • 17
  • 26
  • Possible duplicate of [What is a Null Pointer Exception, and how do I fix it?](http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it) – Rohit Arya Feb 12 '16 at 11:21
  • I know what a null pointer is, I explained in my question that there is a null pointer. I amnt looking for anyone to solve the NULL pointer, I am asking to see if I am calling the methods correctly from within the Zxing onActivityResult function – JJSmith Feb 12 '16 at 11:32

2 Answers2

0

Try like this

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    switch (requestCode) {
        case IntentIntegrator.REQUEST_CODE: {
            if (resultCode != RESULT_CANCELED) {
                IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
                String strData = scanResult.getContents(); // use this
            } else {
                Toast.makeText(this, "Error message", Toast.LENGTH_LONG).show();
            }
            break;
        }
    }
}
Rohit Arya
  • 6,751
  • 1
  • 26
  • 40
0

I have fixed the issue. My problem was that I wasnt opening the database before I was calling the functions and thus the function was a null object. The correct code is as below.

            adapter.open();  // I was missing this line
            Cursor c = adapter.barcodeInfo(bNumber);
            c.moveToPosition(-1);
            while(c.moveToNext())
            {
                name = c.getString(c.getColumnIndex("barcode_item_name"));
                measurement = c.getInt(c.getColumnIndex("barcode_measurement"));
                unit = c.getString(c.getColumnIndex("barcode_unit"));

                Toast toast = Toast.makeText(this, "Adding" + name + "to Kitchen", Toast.LENGTH_SHORT);
                toast.show();
            }
JJSmith
  • 1,833
  • 4
  • 17
  • 26