asdfg
asdfg

Reputation: 27

how to get FirebaseUser data

try to read data from Firebase but get this error:

E/AndroidRuntime: FATAL EXCEPTION: main
   Process: com.example.sozluk, PID: 5798
   java.lang.RuntimeException: java.lang.InstantiationException: Can't instantiate abstract class com.google.firebase.auth.FirebaseUser

my code:

for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {

                   Word2 word= new Word2();
                   word.setMean(postSnapshot.child("mean").getValue(String.class)) ;
                   word.setName(postSnapshot.child("name").getValue(String.class));
                   word.setControlled(postSnapshot.child("controlled").getValue(Boolean.class));
                   word.setAdded_by(postSnapshot.child("added_by").getValue(FirebaseUser.class));
                   word.setKey(postSnapshot.child("key").getValue(String.class));
                   words.add(word);
               }

Upvotes: 0

Views: 133

Answers (1)

Doug Stevenson
Doug Stevenson

Reputation: 317868

This line of code is causing the problem:

word.setAdded_by(postSnapshot.child("added_by").getValue(FirebaseUser.class));

The Firebase SDK can't deserialize an object of type FirebaseUser, because, as the error message says, it's an abstract class. That means it can be created using reflection, which is what the Realtime Database SDK has to do here.

You don't have an easy workaround here. Since it's an abstract class, I suggest not using FirebaseUser directly in any of your classes. You should probably just store the user's UID string instead.

Upvotes: 1

Related Questions