Gradle provides the following class in its API: https://docs.gradle.org/5.6.4/javadoc/org/gradle/api/tasks/ScalaRuntime.html
This class has a simple constructor which accepts a Project
instance. However, in my case I don't have a Project
on hand, but I still want to use other methods of this class which don't really depend on this value, so I decided to pass a null
value to the constructor:
val scalaRuntime = ScalaRuntime(null)
However, the compiler fails with the following error:
Null can not be a value of a non-null type Project
I'm not sure what happens here, because clearly this is a Java class, not Kotlin, and there are no nullability annotations. How come Kotlin rejects this code? I'd assume that it would expect a value of type Project!
(i.e. platform type), not a non-nullable type.
The only way to work around this now is to use reflection to invoke this constructor, which naturally works perfectly fine, so it's not like this constructor expects a truly non-nullable parameter.
What am I missing here? Why Kotlin assumes that the constructor argument is non-null?
@Nullable
, so Kotlin likely concludes that any methods without the annotation are non-nullable. – Andreas