I'm trying to fetch account data from my Firestore database, but when I call get() on my documents, the added OnSuccessListener and OnFailureListener don't seem to call any of their respective methods.

I've tried switching the collectionReference.document() out with a direct DocumentReference, but I get the same issue.

private List<Account> accounts = new LinkedList<>();

private void fetchAccounts(String[] accountNames) {
        Log.d(TAG, "fetchAccounts: fetching accounts...");

        CollectionReference collectionReference =
                FirebaseFirestore.getInstance().collection("accounts");

        for (String name : accountNames) {
            collectionReference.document(name).get().addOnSuccessListener(new OnSuccessListener<DocumentSnapshot>() {
                @Override
                public void onSuccess(DocumentSnapshot documentSnapshot) {
                    if (documentSnapshot.exists()) {
                        Log.d(TAG, "onSuccess: account fetched");

                        accounts.add(documentSnapshot.toObject(Account.class));
                    } else {
                        Log.d(TAG, "onSuccess: document doesn't exist");
                    }
                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception e) {
                    Log.d(TAG, "onFailure: something went wrong");
                }
            });
        }
        Log.d(TAG, "fetchAccounts: " + accounts.get(0).getType());
    }

This is my Account class:

public class Account {
    private int balance;
    private AccountType type;
    private String accountNumber, registrationNumber, owner;

    public Account() {
    }

    public int getBalance() {
        return balance;
    }

    public AccountType getType() {
        return type;
    }

    public String getAccountNumber() {
        return accountNumber;
    }

    public String getRegistrationNumber() {
        return registrationNumber;
    }

    public String getOwner() {
        return owner;
    }
}

AccountType is a simple enum.

It's supposed to either succeed and add the accounts to my list or fail and give me some kind of log output, but it seems to skip that part and instead throw a IndexOutOfBoundsException, when I call on my list's elements.

0 Answers