2

so I have a database where I search for products by barcodes. enter image description here

I wanted to have a check to see if the products are in my database

mFirebaseInstance = FirebaseDatabase.getInstance();
mFirebaseDatabase = mFirebaseInstance.getReference("ProductData");
productId = mFirebaseDatabase.push().getKey();

public void searchProduct(String barcode){

    Query barcodeQuery = mFirebaseDatabase.child("Products")
            .orderByChild("barcode").equalTo(barcode);

    ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for(DataSnapshot ds : dataSnapshot.getChildren()) {

                    String name = ds.child("name").getValue(String.class);
                    int price = ds.child("price").getValue(Integer.class);
                    int quantity = ds.child("quantity").getValue(Integer.class);
                    Log.d("DATABASE", name + "/" + price + "/" + quantity);  /// The output that I get is this: Neopraxam/25/1    and  Vazelina/250/1

That's how I search for the products by barcode.

searchProduct("123123123");

searchProduct("3232");

I have tried to add these methods to check if the value I'm trying to get is null:

if (ds.exists()){}  OR
if (ds.getChildrenCount() != 0){}

But I have seen that it doesn't even enter in the loop if the value doesn't exist. So I'm assuming that It's coming from the query. So, how can I check if the query returns a null value because I used the method .equalTo(barcode) so I suppose that it should return a true or a false value

Adrian
  • 178
  • 1
  • 1
  • 13

2 Answers2

1

Issue seem to be with onDataChange signature as it contains NonNull annotation. To execute onDataChange body even for DataSnapshot null value , update the signature as follows

   ValueEventListener valueEventListener = new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
        .
        .
        .

        }

Note: You should be using SingleValueEventListener instead of ValueEventListener

Ali Ahsan
  • 985
  • 11
  • 16
1

There is no way in which DataSnapshot's getChildren() method can return null:

Returns

  • True if the snapshot contains a non-null value, otherwise false

What you should use to solve the issue, is DataSnapshot's exists() method:

Returns

  • True if the snapshot contains a non-null value, otherwise false

So every time you perform a Query, check each item for existence using the above exists() method.

Besides that, your searchProduct() method can never return a value from the database. For more information, please see my answer from the following posT:

Community
  • 1
  • 1
Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • Thank you. I already solved. I removed the @nonull anotation and I checked dataSnapshot.exist() above the loop because I was checking ds.exist() inside the loop which was bad because there was no ds if there was no DataSnapshot to get childrens from – Adrian Apr 09 '20 at 10:06