I am trying to save the data to realtime database of firebase. But I am getting this error
com.google.firebase.database.DatabaseException: No properties to serialize found on class java.lang.StringBuilder
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper$BeanMapper.<init>(CustomClassMapper.java:548)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.loadOrCreateBeanMapperForClass(CustomClassMapper.java:330)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:167)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.serialize(CustomClassMapper.java:142)
at com.google.firebase.database.core.utilities.encoding.CustomClassMapper.convertToPlainJavaTypes(CustomClassMapper.java:61)
at com.google.firebase.database.DatabaseReference.setValueInternal(DatabaseReference.java:282)
at com.google.firebase.database.DatabaseReference.setValue(DatabaseReference.java:159)
I my earlier projects I used the same code but now I am getting this error.
Here is the code of the function that I used for saving the data
private fun saveDetails() {
val userDataRef: DatabaseReference =
FirebaseDatabase.getInstance().reference.child("UsersData").child(currentUserId)
val userMap = HashMap<String, Any>()
userMap["name"] = name
userMap["email"] = email
userMap["imageUrl"] = profileImageUrl
userMap["phoneNumber"] = number
userDataRef.setValue(userMap).addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(
baseContext,
"$name has successfully registered with email id $email.",
Toast.LENGTH_SHORT
).show()
startActivity(Intent(this, MainActivity::class.java))
} else {
Toast.makeText(baseContext, task.result.toString(), Toast.LENGTH_SHORT).show()
Log.e("Task Unsuccessful", task.result.toString())
}
}
}
>Solution :
it seems that Firebase is having trouble serializing one of the values in your userMap. In this case, it’s likely that one of your values is of type java.lang.StringBuilder instead of java.lang.String.
To resolve the issue make sure all the values you put into the userMap are of type String. If any of them are StringBuilder, you should call the toString() method to convert them to a String.
private fun saveDetails() {
val userDataRef: DatabaseReference =
FirebaseDatabase.getInstance().reference.child("UsersData").child(currentUserId)
val userMap = HashMap<String, Any>()
userMap["name"] = name.toString()
userMap["email"] = email.toString()
userMap["imageUrl"] = profileImageUrl.toString()
userMap["phoneNumber"] = number.toString()
userDataRef.setValue(userMap).addOnCompleteListener { task ->
if (task.isSuccessful) {
Toast.makeText(
baseContext,
"$name has successfully registered with email id $email.",
Toast.LENGTH_SHORT
).show()
startActivity(Intent(this, MainActivity::class.java))
} else {
Toast.makeText(baseContext, task.result.toString(), Toast.LENGTH_SHORT).show()
Log.e("Task Unsuccessful", task.result.toString())
}
}
}