I'm writing some code in Kotlin that throws a NullPointerException on purpose (don't matter why).
Problem is IntelliJ won't even let me compile it.
var x: MyClass? = MyClass()
x = null
var y = x!!.myProperty
I tried using @Suppress("ALWAYS_NULL", "UNREACHABLE_CODE") inline and on the whole file @file:Suppress("ALWAYS_NULL", "UNREACHABLE_CODE") but it still won't let me compile it.
How to compile it?
EDIT:
Actually I thought it was IntelliJ but even trying to compile it manually won't work. I guess the compiler is smarter than me and using !! isn't so assertive as I thought it would be.
CodePudding user response:
You can make something like this.
private var Nothing.myProperty: String
get() = myProperty
set(value){
myProperty = "null"
}
fun main() {
var x: MyClass? = MyClass()
var y = x!!.myProperty
}
class MyClass {
var myProperty: String? = null
}
Compiling without problem. I think if class is null then is better to make if...else checker But more problems bring if property in class somehow will be null
