https://kotlinlang.org logo
Title
n

nickk

08/13/2018, 2:15 PM
When the
photo
property is null, hasPhoto() seems to return true and control goes to the next statement (Kotlin 1.2.60)
c

Czar

08/13/2018, 2:24 PM
if you assign
null
to
photo
it becomes initialized, so this is correct. Although I wonder, how did you assign null to non-nullable property? Reflection?
n

nickk

08/13/2018, 2:30 PM
Well, the property is not nullable, but my API sends a null value. So Gson apparently assigns null to the property and things go bad.
d

Denis A

08/13/2018, 2:31 PM
Is it necessary to use lateinit in this example?
n

nickk

08/13/2018, 2:32 PM
I did it to avoid using ?. or !!, but I think I will remove it.
d

Denis A

08/13/2018, 2:32 PM
Optional value contain native analog “has…” )
I think, best way is using clean optional value in this example
👍 1
i

ilya.gorbunov

08/13/2018, 6:17 PM
Does it work if you remove
open
modifier?
t

Tony Hoyle

08/13/2018, 6:20 PM
If you've got an external API setting it to null anyway, using lateinit to avoid ?. just opens you up to problems because you've bypassed the null safety kotlin gives you
3
n

nickk

08/14/2018, 7:55 AM
@Tony Hoyle Yes, I’ve changed it to nullable.
lateinit
was a really bad idea in this case. I hadn’t thought that gson could set it to null anyway.