var a: String? = "abc" // can be set null a = null // value set to null val l = if (a != null) a. To avoid this error we have to check if b is null and handle two cases in a respective manner. To allow nulls, we have to declare a variable as a nullable string, which can be done by adding a “?” to the data type i.e, “String? ”.The nullable counterpart of some type A is denoted as A? var a: String? = "abc" // this is a nullable string b = null // value is set to null print(a) // null printedīut in this case, there are chances of NPE error when a method or property is accessed. In Kotlin a normal data type like string cannot store a null value and trying to do so will cause an error. This is done to eliminate the NullPointerException error in the code which is common in other programming languages like Java. Also, I almost never use null in idiomatic Kotlin programming. In Kotlin by default, the data types are of non-nullable types i.e trying to assign a value as null would cause an error. Kotlins nullable types and Option are exactly the same thing, except with some implicit.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |