0

I'm trying get data from Firestore. I get the data successfully.

Log.i("FirestoreClass", "$document")

resulted in the document from the firestore. But while coverting it to a object of User class:

@Parcelize
data class User(
    var id : String?,
    var email : String?,
    var profileHeadline : String? = "",
    var profileSummary : String? = "",
    var profilePhoto : String? = "",
    var personalInfo: PersonalInfo? = PersonalInfo(),
    var education: List<Education>? = listOf(Education()),
    var experience: List<Experience>? = listOf(Experience()),
    var skills : List<Skills>? = listOf(Skills()),
    var languages: List<Languages>? = listOf(Languages()),
    var interest : List<Interest>? = listOf(Interest()),
    var awardAndAchievements: List<AwardAndAchievements>?
    = listOf(AwardAndAchievements()),
    var licenceAndCertification: List<LicenceAndCertification>?
    = listOf(LicenceAndCertification()),
    var projects : List<Projects>? = listOf(Projects()),
    var publications: List<Publications>? = listOf(Publications()),
    var patents : List<Patents> = listOf(Patents()),
    var volunteerExperience : List<VolunteerExperience>?
    = listOf(VolunteerExperience()),
    var recommendations: List<Recommendations>? = listOf(Recommendations()),
    var customSectionOneLine : List<CustomSectionOneLine>?
    = listOf(CustomSectionOneLine()),
    var customSectionTwoLine : List<CustomSectionTwoLine>?
    = listOf(CustomSectionTwoLine()),
    var fcmToken : String? = ""
) : Parcelable

I got an error in the logcat.

2022-09-02 19:12:04.826 7195-7195/com.example.theresumebuilder E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.theresumebuilder, PID: 7195
    java.lang.RuntimeException: Could not deserialize object. Class com.example.theresumebuilder.models.User does not define a no-argument constructor. If you are using ProGuard, make sure these constructors are not stripped
        at com.google.firebase.firestore.util.CustomClassMapper.deserializeError(CustomClassMapper.java:568)
        at com.google.firebase.firestore.util.CustomClassMapper.access$200(CustomClassMapper.java:57)
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:754)
        at com.google.firebase.firestore.util.CustomClassMapper$BeanMapper.deserialize(CustomClassMapper.java:746)
        at com.google.firebase.firestore.util.CustomClassMapper.convertBean(CustomClassMapper.java:547)
        at com.google.firebase.firestore.util.CustomClassMapper.deserializeToClass(CustomClassMapper.java:258)
        at com.google.firebase.firestore.util.CustomClassMapper.convertToCustomClass(CustomClassMapper.java:103)
        at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:183)
        at com.google.firebase.firestore.DocumentSnapshot.toObject(DocumentSnapshot.java:161)
        at com.example.theresumebuilder.firebase.FirestoreClass.getUserFromDatabase$lambda-2(FirestoreClass.kt:44)
        at com.example.theresumebuilder.firebase.FirestoreClass.$r8$lambda$e6ebAYLyyLOhg0E7gq6xcXf2etE(Unknown Source:0)
        at com.example.theresumebuilder.firebase.FirestoreClass$$ExternalSyntheticLambda2.onSuccess(Unknown Source:4)
        at com.google.android.gms.tasks.zzm.run(com.google.android.gms:play-services-tasks@@18.0.1:1)
        at android.os.Handler.handleCallback(Handler.java:790)
        at android.os.Handler.dispatchMessage(Handler.java:99)
        at android.os.Looper.loop(Looper.java:164)
        at android.app.ActivityThread.main(ActivityThread.java:6494)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

This is the code for Firestore:


fun getUserFromDatabase(activity: LoginActivity){

        db.collection(Constants.USERS)
            .document(getCurrentUUID())
            .get()
            .addOnSuccessListener {document->
                Log.i("FirestoreClass","${document.toObject(User::class.java)}")
                val loggedInUser = document.toObject(User::class.java)
                activity.userDataFromDatabase(loggedInUser)
            }
            .addOnFailureListener {
                activity.hideProgressDialog()
                Toast.makeText(
                    activity,
                    it.message,
                    Toast.LENGTH_SHORT
                ).show()
            }
    }

I tried my best to find the way to solve this error but unable to find it. I'm tryna build a resume builder app. I think there is something with the User class, I've used many data classes to save the data of the user And at last compounded them in the User data class. If you need anything else. Please let me know. Thanks in advance.

Navneet
  • 77
  • 6
  • Focus on the error message "Class com.example.theresumebuilder.models.User does not define a no-argument constructor.". Your User class need a no-argument constructor in order to be automatically deserialized. – Doug Stevenson Sep 02 '22 at 14:28
  • 1
    Assign default values to id and email like this. It would work var id : String? =null, var email : String?=null – Gowtham K K Sep 02 '22 at 14:31
  • Thanks a lot! @GowthamKK that worked !! But I didn't understand why it was not working before. Can you elaborate the concept? – Navneet Sep 02 '22 at 16:22
  • 2
    Reason is because, while deserializing it will create User() object without any value. It will call primary empty constructor. Here in your code when it tries to create empty constructor it fails because id and email is mandatory in constructor as it does not have any default.If you assign all fields in data class with value or null, it will create User object with empty constructor and the assign values. You can also solve this be using normal class and all the fields outside constructor. So it will create empty object and assign values while deserializing. Hope you got it. – Gowtham K K Sep 02 '22 at 17:44
  • Since you're using Kotlin, I think that this [resource](https://medium.com/firebase-tips-tricks/how-to-read-data-from-cloud-firestore-using-get-bf03b6ee4953) will help. – Alex Mamo Sep 03 '22 at 06:42

0 Answers0