There might be too much to read or the documentation is unclear or even worse: out of date. But as we all know, consulting the documentation is not always the developer’s first approach. The first and probably easiest way would be to write a comment which specifies the valid inputs. We have a few different options to avoid such unpleasant surprises: 1️⃣ Documentation Especially when it’s not as obvious as in our example which input values are valid and which ones aren’t.įor example, would you be able to tell how valid input arguments look like for this weird looking signature? fun request( It’s not the end of the world, but still pretty annoying. Then suddenly, oh wonder, an arithmetic exception is thrown, and the execution of the program stops. fun List.average() = sum() / sizeĮverything works perfectly well until someone calls this function on an empty list. Normally,types of String are not nullable.As a concrete example in Kotlin, let’s have a look at the following average function, where we divide the sum of all elements in a list by the list’s size. Kotlin types system differentiates between references which can hold null (nullable reference) and which cannot hold null (non null reference).
Kotlin Nullable Types and Non-Nullable Types
Kotlin's type system is aimed to eliminate NullPointerException form the code.
Kotlin compiler throws NullPointerException immediately if it found any null argument is passed without executing any other statements. Kotlin null safety is a procedure to eliminate the risk of null reference from the code.