A
A
Ara Gevorgyan2019-03-13 03:21:12
Android
Ara Gevorgyan, 2019-03-13 03:21:12

Error when working with arrays in Cloud Firestore?

Hello! I ran into trouble, the point is that I wrote the code to get the value from the array from the database and after deleting this value from the database, everything works fine until the database is empty, after that it crashes with an error pointing to the line that takes the first value from array, that is, as I understand it, the application completely ignores the if (promocodes != null) variable and perceives "[]" as a non-zero value, how can I fix it so that when the array field is empty, it does not execute the commands that go to if (promocodes != null)?
application code itself:

public class MainActivity extends AppCompatActivity {

    private static final String TAG = "MainActivity";

    Button mButtonGive;
    Button mButtonGiveIvi;
    TextView mTextPromo;
    TextView mTextPromoIvi;

    FirebaseFirestore mRef = FirebaseFirestore.getInstance();
    DocumentReference mDelRef = mRef.collection("Promocode").document("Delivery");
    DocumentReference mIviRef = mRef.collection("Promocode").document("Ivi");


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mButtonGive = findViewById(R.id.mButtonGive);
        mButtonGiveIvi = findViewById(R.id.mButtonGiveIvi);
        mTextPromo = findViewById(R.id.mTextPromo);
        mTextPromoIvi = findViewById(R.id.mTextPromoIvi);

        mButtonGive.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDelRef.get().addOnCompleteListener(new OnCompleteListener<DocumentSnapshot>() {
                    @Override
                    public void onComplete(@NonNull Task<DocumentSnapshot> task) {
                            if (task.isSuccessful()) {
                                DocumentSnapshot document = task.getResult();
                                assert document != null;
                                if (document.exists()) {
                                    Log.d(TAG, "DocumentSnapshot data: " + document.getData());
                                    List<String> promocodes;
                                    promocodes = (List<String>) document.get("Promocode");
                                    Log.d(TAG, "Promocodes: " + promocodes);
                                    if (promocodes != null) {
                                        mTextPromo.setText(promocodes.get(0));
                                        mDelRef.update("Promocode", FieldValue.arrayRemove(promocodes.get(0)));
                                    } else {
                                        mTextPromo.setText("No Promo");
                                        Log.d(TAG, "No Promo");
                                    }
                                } else {
                                    Log.d(TAG, "No such document");
                                }

                            }
                            else {
                                Log.d(TAG, "get failed with ", task.getException());
                            }
                    }

                });
            }
        });

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Denis Zagaevsky, 2019-03-13
@zagayevskiy

It's Java, not Javascript, get used to it. An empty list (not an array! They are different things!) is NOT null. Check for isEmpty().

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question