3

I'm trying to call Java method org.springframework.data.mongodb.core.query.Update#set from Kotlin code. This method is declared as

public Update set(String key, Object value)

There is no additional annotations or comments about parameters nullability. In fact, it just puts value into HashMap, so effectively value might be null.

However, Kotlin compiler treats value type as Any (instead of expected Any!), and fails when passing null with

Null can not be a value of a non-null type Any

Passing nullable value with !! patently causes KotlinNullPointerException, casting as Any throws TypeCastException.

What is the logic behind such nullability inference? Passing null parameter with reflection actually works well, but is there more natural solution?

1

1 Answer 1

6

This is caused by the annotation in the package's package-info.java:

@org.springframework.lang.NonNullApi
package org.springframework.data.mongodb.core.query;

which is

A common Spring annotation to declare that parameters and return values are to be considered as non-nullable by default for a given package... used by Kotlin to infer nullability of Spring API

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.