# Kotline Null safety

**URL:** <https://forums.kodeco.com/t/kotline-null-safety/138382>\
**Category:** Kotlin Apprentice\
**Created:** [April 22, 2021, 7:20pm UTC](https://forums.kodeco.com/t/kotline-null-safety/138382 "2021-04-22T19:20:08Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ashishsmse14](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ashishsmse14/32/81494_2.png) [@ashishsmse14](https://forums.kodeco.com/u/ashishsmse14)\
**Post date:** [April 22, 2021, 7:20pm UTC](https://forums.kodeco.com/t/kotline-null-safety/138382/1 "2021-04-22T19:20:08Z")

</div>

We need to create function named as nullable which will accept nullable string as a parameter. Return length of the string , if NULL then -1… task as per it written code

```
import java.io.*
import java.math.*
import java.security.*
import java.text.*
import java.util.*
import java.util.concurrent.*
import java.util.function.*
import java.util.regex.*
import java.util.stream.*
import kotlin.collections.*
import kotlin.comparisons.*
import kotlin.io.*
import kotlin.jvm.*
import kotlin.jvm.functions.*
import kotlin.jvm.internal.*
import kotlin.ranges.*
import kotlin.sequences.*
import kotlin.text.*

--- Editable code as ---
fun nullable(s1: String): Units{
 var b: String? = s1
b=null
val l = b?.length ?: -1
print(l)
return(l)
  }
--- End ---

fun main(args: Array<String>) {
    val str = readLine()!!
        var result=-100
    if(str=="null"){
         result = nullable(null)
    }else
        result = nullable(str)
    println(result)
}

```

We getting error as

- Solution.kt:22:27: error: unresolved reference: Units fun nullable(s1: String): Units{ ^

- Solution.kt:34:28: error: null can not be a value of a non-null type String result = nullable(null) ^

---

<div class="post-metadata">

**Author:** ![vgonda](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vgonda/32/102490_2.png) [@vgonda](https://forums.kodeco.com/u/vgonda)\
**Post date:** [April 23, 2021, 12:14am UTC](https://forums.kodeco.com/t/kotline-null-safety/138382/2 "2021-04-23T00:14:29Z")

</div>

Thanks for the question, @ashishsmse14 !

Instead of:

```kotlin
fun nullable(s1: String): Units{

```

Use:

```kotlin
fun nullable(s1: String?): Unit{

```

There are two differences here:

1. Use `Unit` without the “s” at the end. You could also optionally omit `: Unit` as it it the default return type for the function.
2. Use `String?` with a “?” for the parameter type. This makes the parameter nullable.

I hope this helps! Please let us know if you have any questions about the book.

---

<div class="post-metadata">

**Author:** ![ashishsmse14](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ashishsmse14/32/81494_2.png) [@ashishsmse14](https://forums.kodeco.com/u/ashishsmse14)\
**Post date:** [April 23, 2021, 2:48am UTC](https://forums.kodeco.com/t/kotline-null-safety/138382/3 "2021-04-23T02:48:45Z")

</div>

thanks -

```
fun nullable(s1: String?): Unit{
var b: String? = s1
b=null
val l = b?.length ?: -1
print(l)
return(l)
}

```

We now getting error as -

```auto
Solution.kt:37:8: error: type mismatch: inferred type is Int but Unit was expected
return(l)
       ^
Solution.kt:44:19: error: type mismatch: inferred type is Unit but Int was expected
         result = nullable(null)
                  ^
Solution.kt:46:18: error: type mismatch: inferred type is Unit but Int was expected
        result = nullable(str)

```

---

<div class="post-metadata">

**Author:** ![ashishsmse14](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/ashishsmse14/32/81494_2.png) [@ashishsmse14](https://forums.kodeco.com/u/ashishsmse14)\
**Post date:** [April 23, 2021, 6:45am UTC](https://forums.kodeco.com/t/kotline-null-safety/138382/4 "2021-04-23T06:45:20Z")

</div>

issue resolved thanks

---

<div class="post-metadata">

**Author:** ![vgonda](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vgonda/32/102490_2.png) [@vgonda](https://forums.kodeco.com/u/vgonda)\
**Post date:** [April 27, 2021, 12:38am UTC](https://forums.kodeco.com/t/kotline-null-safety/138382/5 "2021-04-27T00:38:21Z")

</div>

I’m glad your issue is resolved! Thanks for reaching out.
