```try { myObject.theField } catch(t: Throwabl...
# announcements
r
Copy code
try {
    myObject.theField
} catch(t: Throwable) {
    log.error("theField is null")
}
h
can you provide the declaration of your data class please?
Copy code
data class Foo(val name: String)
val myFoo = Foo(null)
does not compile, there is no way of getting a Foo with a null name - the compiler prevents that.
r
There are multiple obvious ways to have null values in non null fields
h
show me one 🙂
r
I guess the most obvious one is reflection
In my specific case it's Moshi
Retrofit with a MoshiConverter
h
ok, so we are talking about Interop issues
r
Most things running on the JVM do not care about nullability
h
I wasn't aware you were talking about "kotlin-only"
r
You can't do much with Kotlin only 😛
h
ok, I just did a quick test with a simple static Java method that returns a null String reference.
myObject.theField = JavaTester.returnNull()
throws an
IllegalStateException
oops, one moment... 🙂
reflection might work though
r
You need either reflection or the Java part stating it doesn't return null using annotations while still returning null
A simple Java method returning a
String
is considered to return a
String?
to a Kotlin caller
h
exactly
r
Now if you put
@NotNull
on your Java method and still return null, Kotlin is lost
h
which @NotNull exactly? There are many.
r
Any of those understood by the Kotlin compiler I guess
h
I tried org.jetbrains.annotations.NotNull
r
Which should be the same as the ones understood by IDEA by default
h
still IllegalStateException
r
The Java part needs to be compiled separately
h
org.jetbrains.annotations.NotNull is the IDEA default, yeah
r
It needs to be a compiled library
h
ok, I see
r
If it's your own file that you compile at the same time, then it knows
Another problem is that you really never know how all the alibraries you use work, they may use reflection and you don't know it
My problem on the Kotlin side is that Kotlin never checks for that null. This object held a null value for days in the RAM, then at some point somebody did an action which passed this value to a method, which doesn't take nulls, and boom. It should have failed earlier. It was very hard to trace
h
I see. You might want to ask again in the channel and provide a little more background.