Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I serialize my User class for the Firebase Database and fix this error?

I'm trying to retrieve the updated User from the dataSnapshot of the Firebase Database:

mRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            User atualizedUser = dataSnapshot.getValue(User.class);
            fName.setText(atualizedUser.getFirstName());
            lName.setText(atualizedUser.getLastName());
            profilePhoto.setProfileId(atualizedUser.getProfileId());
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });
}

But I get this error below:

com.google.firebase.database.DatabaseException: Class bqs.bomqueso.Model.User is missing a constructor with no arguments
                                                            at com.google.android.gms.internal.zzbtg$zza.zze(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg$zza.zzaH(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg.zze(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg.zzb(Unknown Source)
                                                            at com.google.android.gms.internal.zzbtg.zza(Unknown Source)
                                                            at com.google.firebase.database.DataSnapshot.getValue(Unknown Source)
                                                            at bqs.bomqueso.Activities.MainActivity$3.onDataChange(MainActivity.java:92)
                                                            at com.google.android.gms.internal.zzbpx.zza(Unknown Source)
                                                            at com.google.android.gms.internal.zzbqx.zzZS(Unknown Source)
                                                            at com.google.android.gms.internal.zzbra$1.run(Unknown Source)
                                                            at android.os.Handler.handleCallback(Handler.java:746)
                                                            at android.os.Handler.dispatchMessage(Handler.java:95)
                                                            at android.os.Looper.loop(Looper.java:148)
                                                            at android.app.ActivityThread.main(ActivityThread.java:5443)
                                                            at java.lang.reflect.Method.invoke(Native Method)
                                                            at     com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
                                                            at     com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)

Here my User class (this is not a subclass, I created it in a separate file):

public class User {

private String firstName;
private String lastName;
private String profileId;

public User() { }

    public User(String fName, String lName, String id) {
        this.firstName = fName;
        this.lastName = lName;
        this.profileId = id;
    }

    public String getProfileId() {
        return profileId;
    }

    public void setProfileId(String id) { 
        this.profileId = id; 
    }

    public String getLastName() {
        return lastName;
    }

    public void setLastName(String lName) { 
        this.lastName = lName; 
    }

    public String getFirstName() {
        return firstName;
    }

    public void setFirstName(String fName) { 
        this.firstName = fName; 
    }
}

I find similar questions here, but I hadn't found a solution for my problem, I hope you guys can help me, Thank you in advance!

like image 258
Andre Goncalves Avatar asked Mar 23 '17 06:03

Andre Goncalves


People also ask

How do I save and recover data from Firebase?

Go to the Firebase Console, select your project and click on the Database tab. Scroll down and you will find a Realtime Database section. Click on Create database, select the Start in test mode option and then click Enable.

How many users Firebase can handle?

The limit you're referring to is the limit for the number of concurrently connected users to Firebase Realtime Database on the free Spark plan. Once you upgrade to a payment plan, your project will allow 200,000 simultaneously connected users.

How can you specify what place to update in Firebase realtime database?

If you want to allow users to update their profiles you could update the username as follows: FirebaseDatabase database = FirebaseDatabase. getInstance(); DatabaseReference mDatabaseRef = database. getReference(); mDatabaseRef.


2 Answers

For Kotlin, if you're using data class like below for firebase,

data class UserModel(
        var userName: String,
        var userType: Int
)

then you'll get an error like

com.google.firebase.database.DatabaseException: UserModel does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped.

So for this, you just need to initialize its values.

data class UserModel(
        var userName: String = "",
        var userType: Int = 0
)
like image 139
Kishan Solanki Avatar answered Oct 08 '22 11:10

Kishan Solanki


Create a constructor with no arguments as

public User() { } as cricket mentioned and to serialize

public class User implements Serializable {

private String firstName;
private String lastName;
private String profileId;

public User() {
}

public User(String fName, String lName, String id) {
    this.firstName = fName;
    this.lastName = lName;
    this.profileId = id;

}
public String getProfileId() {
    return profileId;
}

public void setProfileId(String id) {
    this.profileId = id;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lName) {
    this.lastName = lName;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String fName) {
    this.firstName = fName;
}

}

If this didn't meet requirement note down

like image 24
g7pro Avatar answered Oct 08 '22 11:10

g7pro