'Kotlin : Type mismatch when class implement Serializable
I have a project that was written in both Java & Kotlin (forgive my very newbie kotlin understanding)
The class I would like to serialize has implemented the Serializable interface.
Error Message:
Type mismatch: inferred type is EntityRelationship but Serializable! was expected
public class EntityRelationship implements Serializable {
@NonNull
private Relationship relationship;
@Builder.Default
private List<Relationship> childRelationship = new ArrayList<>();
}
When I tried to serialize it
val entityRelationship: EntityRelationship = relationshipList.get(0)
val serializedResponse = SerializationUtils.serialize(entityRelationship) //<--Trouble in this line
I do not understand why type mismatch happen here, because entityRelationship already implement serializable interface. Even if I enforce
val entityRelationship: Serializable = relationshipList.get(0)
p.s: relationshipList is a List
I still get the same error. My zero understanding regards to kotlin forbid me to ping the issue. Any pointer is appreciated.
Solution 1:[1]
First of all "Relationship" class should be implemented Serializable as well. And you may need to make your serialize function generic and cast your class in SerializationUtils.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Berkay KireƧci |
