Search code examples
javaserializationcompiler-errorsincompatibletypeerrorvariable-types

Java List deserializing and returning an object gives Incompatible types error


I'm trying to deserialize a SetOfMembers list,and I want to take each Member object individually after deserializing in order to match the member name with a given String.

"Incompatible types librarby.Member cannot be converted to Member where Member is a type-variable. Member extends Object declared in class SetOfMembers"

The above error keeps appearing and I cannot make out the problem in my code.If you could explain to me what's wrong my code that'll be great. I used the @SuppressWarnings("unchecked") but that's not helpful either.Please help. Thankyou

Error


Solution

  • My first guess would be that

    1. Top and bottom pieces of code are in different files
    2. In the "top" file you imported (most probably by IDE suggestion) some other Member class such as java.lang.reflect.Member instead of your library.Member and thus you have this error.

    So to fix this - check your imports section in the first tile.

    If you really need both Member classes in the same file, you can still use fully qualified names. I bet following code would work anyway.

    for(library.Member mem : Serialization.deserializeMembers())