Skip to content Skip to sidebar Skip to footer

Can't Pass Null For Argument 'pathstring' In Child()

I'm currently trying to read the contents from my Firebase Database into a RecyclerView but I want to load them from the last node to first node. So I decided to place the keys int

Solution 1:

You're being bitten by asynchronous programming 101. You need to make sure that the code that requires the key[] contents is inside the onDataChange that is called once the keys are loaded.

In this case that means it should look something like thisL

mDatabase.addValueEventListener(newValueEventListener() {
    @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
        int i = 0;
        for (DataSnapshot d : dataSnapshot.getChildren()) {
            key[i] = d.getKey();
            i++;
        }
        for (int counter = key.length - 1; counter >= 0; counter--) {
            viewHolder.setUpDate(first + "/" + key[counter]);
            mDatabase.child(key[counter]).addValueEventListener(newValueEventListener() {
                @OverridepublicvoidonDataChange(DataSnapshot dataSnapshot) {
                    StringBuilder builder = newStringBuilder();
                    for (DataSnapshot child : dataSnapshot.getChildren()) {
                        builder.append(child.getKey().toString() + "\n\n");

                    }
                    viewHolder.setName(builder.toString());
                }

                @OverridepublicvoidonCancelled(DatabaseError databaseError) {
                    throw databaseError.toException(); // don't ignore errors        
                }
            });
        }
    }

    @OverridepublicvoidonCancelled(DatabaseError databaseError) {
        throw databaseError.toException(); // don't ignore errors
    }
});

Post a Comment for "Can't Pass Null For Argument 'pathstring' In Child()"