Development environment IDE : android studio OS : mac
--If you use kotlin, nullpo will not occur
The following Java code may be used Map seems to be read-only in kotlin Therefore, a mutableMap that can be changed is used instead.
hoge.java
Map map = HashMap<>
It's revealed that I didn't write it as read-only until now.
hoge.kt
MutableMap map = HashMap<>
It makes me angry every time there is code that seems to contain null So I tried to do a null check just before that code
However, there is an Elvis operator that throws an exception when the value is null. It seems that it was possible to prevent the amount of writing if statements from increasing.
before.kt
val hoge = func()
if(hoge!=null){
hoge.fuga()
}
after.kt
func()?.fuga() ?: throw Exception("Akan")
?. is a safe call It seems to return null if the previous expression was null