I have searched this in multiple places but I haven't found or understood the best approach this issue.
I have a query in Jooq with a left join, which results in rows being returned but with null values.
When it attempts to map it, it throws the following error:
java.lang.NullPointerException: null cannot be cast to non-null type kotlin.String
One way to fix this would be to make the generated code by jooq(getters) as nullable
Another approach will be to surround them by try/catch clause but i don't like it as I might have to do in multiple places.
Is there a cleaner approach? (apologies for any error in the code snippets as they have been changed to make it simpler)
fun findById(id: String): User? {
return dslContext.select()
.from(User).leftJoin(USER_NAME).on(USER_NAME.ID.eq(USER.ID))
.where(USER.ID.eq(id))
.fetchGroups(USER, USER_NAME)
.map { mapUserAndUserName(it) }
.firstOrNull()
}
private fun mapUserAndUserName(mapItem: Map.Entry<UserRecord, Result<UsernameRecords>>): User {
val record = mapItem.key
val usernames = mapItem.value.mapNotNull { it?.username } // this is where i get the error
return mapUser(record, tags)
}
Code generated by Jooq
open var tag: String
set(value): Unit = set(1, value)
get(): String = get(1) as String