Error:(55, 41) Kotlin: Type inference failed. Expected type mismatch

#1

Error:(55, 41) Kotlin: Type inference failed. Expected type mismatch: inferred type is (Mutable)List<Optional<Address!>!>! but List? was expected
I save the data to a mongoDB Document my build.gradle has:
implementation ‘org.springframework.boot:spring-boot-starter-actuator’
implementation ‘org.springframework.boot:spring-boot-starter-data-mongodb’
implementation ‘org.springframework.boot:spring-boot-starter-data-rest’
implementation ‘org.springframework.boot:spring-boot-starter-hateoas’
implementation ‘org.springframework.boot:spring-boot-starter-web’
implementation ‘com.fasterxml.jackson.module:jackson-module-kotlin’

My Document Objects
@Document(collection = “address”)
class Address {
@Id
var id: String? = null
var number: String? = null
var street: String? = null
var neighborhood: String? = null
var locality: String? = null
var region: String? = null
var country: String? = null
var code: String? = null
var google_place_id: String? = null

    constructor()
}

@Document(collection = "person")
class Person {
    @Id
    var id: String? = null
    var name: String? = null
    var lastName: String? = null
    var phone: String? = null
    var email: String? = null
    var password: String? = null
    @DBRef(db = "mlm")
    var address: List<Address>? = null

    constructor()
}

I have interfaces
@Repository
interface PersonRepository : MongoRepository<Person, String>
And
@Repository
interface AddressRepository : MongoRepository<Address, String>

My Controllers work
My Seeder / well test really to see how to do this looks like this and this is where I encounter the problem
class DbSeeder {

    @Autowired
    private val personRepository: PersonRepository? = null
    @Autowired
    private val addressRepository: AddressRepository? = null

    fun addressLoading() {
        val address1 = Address()
        address1.id = "5cb2e9424274072ec4bb4199"
        address1.number = "1"
        address1.street = "Microsoft Way"
        address1.neighborhood = "Redmond"
        address1.locality = "King County"
        address1.region = "Washington"
        address1.code = "425"
        address1.country = "United States"
        address1.google_place_id = "5644+83 Centurion"

        val address2 = Address()
        address2.id = "5cb2e9424274072ec4bb4198"
        address2.number = "1600"
        address2.street = "Amphitheatre Parkway"
        address2.neighborhood = ""
        address2.locality = "Mountain View"
        address2.region = "California"
        address2.country = "United States"
        address2.code = "94043"
        address2.google_place_id = "CWC8+Q9 Mountain View, California, USA"

        val address = Arrays.asList(address1, address2)
        this.addressRepository!!.insert(address)
    }

    fun personLoading() {
        val personDocument = Person()
        personDocument.id = "5cb2e9424274072ec4bb4197"
        personDocument.name = "William"
        personDocument.lastName = "Gates"
        personDocument.phone = "1081010810"
        personDocument.email = "bill.gates@gmail.com"
        personDocument.password = "bill-secret"

        val personAddressDBRef = addressRepository!!.findById("5cb2e9424274072ec4bb4199")

        personDocument.address = Arrays.asList(personAddressDBRef)

//        val personDBRef = personRepository!!.save(personDocument)  // If the ObjectID is requires else ware
        personRepository!!.save(personDocument)
    }
}

to save my address I need to find the Address related to the ObjectID then save the ObjectID as a DBRef in the person Document
val personAddressDBRef = addressRepository!!.findById(“5cb2e9424274072ec4bb4199”)
personDocument.address = Arrays.asList(personAddressDBRef)
This is where I get the error “Error:(55, 41) Kotlin: Type inference failed. Expected type mismatch: inferred type is (Mutable)List<Optional<Address!>!>! but List? was expected”
I seem to have no clue how to proceed
I made it available on GitHub
Any help will be apreciated

#2

Could you pleas only post the part of the code with the error, I have no idea where it is :wink:

#3

Seems you missed it ! lol its nicely commented = Almost in the last line and below the code repeated!
However I found an Answer in StackOverflow
Getting Error:(55, 41) Kotlin: Type inference failed

#4

My problem wasn’t that the line with the error was missing. My problem was that you posted about 100 lines of code for something that only requires about 2-3 at max. This means I or anyone else who wants to help you first has to filter out all the unnecessary information.
I’m glad you got your answer, but maybe next time you could try to only post the necessary code and use the code formatting :smiley:

#5

On the other hand as per StackExchange policy witch was developed purely to be precise enough to enable the “Helper” (You) to be able to answer the question without asking more info, thus delay a good answer.
I however cautioned on the side of providing ALL the info I could of provided only the code snip that caused the error
Thanx anyhow

#6

Yeah, but since yours was a type error by the compiler all you need is the statement that created the error and the types/statements that were used to create the used values.

But don’t take my view on this as an absolute. It’s hard to know what information are necessary, so I agree that you should err on the side of too many information, but I think in this case it was still a bit too much.