15

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!

3
  • 2
    User is missing a constructor with no arguments... This doesn't match your code... You do have public User() { } Mar 23, 2017 at 6:51
  • I don't know why I'm having this error, because, as you said I do have a constructor with no arguments, any hints? Mar 23, 2017 at 19:10
  • Try cleaning and rebuilding the project? Mar 24, 2017 at 2:10

2 Answers 2

35

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
)
2

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

4
  • You duplicated the no-arg constructor Mar 24, 2017 at 2:10
  • Now i have corrected My mistake Thanks for the feedback
    – g7pro
    Mar 24, 2017 at 6:47
  • 3
    How is this different from the code in the question, then? Mar 24, 2017 at 13:28
  • What happens if 'android.location.Address' is a type of an attribute in this class, I can't change this class to implement zero argument method, what can I do?
    – Marci-man
    Mar 20, 2019 at 21:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.