Nullability

Nullability is the ability of a data type to become null during the execution time and throw NullPointer Exception to stop/crash the application

Kotlin converts these Nullable problems from runtime into compile time so that user would be able to see the null error during the compile time. So this will avoid the null pointer exception during the execution/run time

What are Non-Nullable DataTypes in kotlin ?

Non-Nullable data types are nothing but normal data types, that we use in day to day life. These data types will never accept a null as a value
Eg : String, Int, Float...

What is Nullable dataType in kotlin ?

Nullable data types are also the same as Normal datatypes but these will accept the null values as well. We have to add question mar at the end of the datatype to make it as Nullable.
EG: String?, Int?, Float?...

How kotlin Avoids NullPointerException :

  • Normal data types don't allow you to store the null
    not-nullable-string
  • Kotlin doesn't allow the function calls on a nullable variable because allowing function calls on a variable which possibly hold a null value, you find it in below example
  • If we create a function with non-nullable type, then no matter what Kotlin will not allow passing a null value to that method. Kotlin compiler finds the null value while passing the parameter and shows error
    cannot-accept-null-kotlin-function
  • If you create a function with nullable type then without checking for null, you cannot access the methods
    nullable-function-parameter-kotlin
  • Corrected above code
    null-check-in-if-block-kotlin-function
    Reasons for NullPointerException ient/" method='post'>
Point our Mistakes and Post Your Suggestions