Kotlin aims to eliminate null and NullPointerException from your code. This is why types are non-null by default. Unfortunately, it's not always possible to avoid null, especially if working within a mixed Java plus Kotlin code base. There are sometimes scenarios where null might be a reasonable option to represent your data, or you might be working with an API that uses null. For these types of cases, there are several ways in which you can safely work with null types, such as the following ones:
- Safe callsĀ
- Non-null assertion
- Conditionals
- The Elvis operator
- Safe casts