# isNotEmpty() method returns null on Nullable string object

**URL:** <https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971>\
**Category:** Language Design\
**Created:** [May 31, 2018, 2:20pm UTC](https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971 "2018-05-31T14:20:08Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![silwar](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/silwar/32/3075_2.png) [@silwar](https://discuss.kotlinlang.org/u/silwar)\
**Post date:** [May 31, 2018, 2:20pm UTC](https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971/1 "2018-05-31T14:20:08Z")

</div>

Hi

Till now I was using isNotEmpty as below

```
       val a : String? = null
       if(a?.isNotEmpty()!!){
                  <do something>
       }

```

As I knew that as per isNotEmpty() method’s definition it will either return true or false but can not return null if a is null. But today i faced NPE with this method.  
Below is isNotEmpty() method’s definition

```
      @kotlin.internal.InlineOnly
      public inline fun CharSequence.isNotEmpty(): Boolean = length > 0

```

please help me to understand if this is _ **Bug** _ or _ **feature** _ or i am missing something here.

Due to this now I am using above code as below

```
       val a : String? = null
       if(a?.isNotEmpty() == true){
                  <do something>
       }

```

I am currently using Android Studio 3.1.2 on Ubuntu 16.04 and Kotlin version is 1.2.41

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [May 31, 2018, 2:38pm UTC](https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971/2 "2018-05-31T14:38:31Z")

</div>

I guess you understand that `a?.isNotEmpty` can return either `null` if a is `null` or the result of `a.isNotEmpty()`. If not you should take another look at the [documentation under Safe Calls](https://kotlinlang.org/docs/reference/null-safety.html#safe-calls).  
So when you use `!!` it will throw a NPE if a is null.

If itself can not handle nullable parameters (I’m not sure where this is explained exactly, I think it is somewhere in the documentation but I can not find it right now). That’s why you have to use the comparison.

```kotlin
null == true => false
false == true => false
true == true => true

```

You could also create your own utility

```kotlin

fun String?.isNotNullNorEmpty() =
	this?.isNotEmpty() == true
// or
fun String?.isNotNullNorEmpty() =
	this?.isNotEmpty() ?: false

```

Btw, once you understand safe calls properly I would prefer my second utility as it is easier to understand IMO, considering how safe calls work. But I guess that is preference and not universally right.

---

<div class="post-metadata">

**Author:** ![silwar](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/silwar/32/3075_2.png) [@silwar](https://discuss.kotlinlang.org/u/silwar)\
**Post date:** [May 31, 2018, 3:05pm UTC](https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971/3 "2018-05-31T15:05:50Z")

</div>

@Wasabi375 Thanks for quick reply. How can I miss this silly point that if string is null then isNotEmpty() will not be executed at all. Also thanks for utility methods, I will implement these. 🙂

---

<div class="post-metadata">

**Author:** ![Mike\_Wilkes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mike_wilkes/32/2620_2.png) [@Mike\_Wilkes](https://discuss.kotlinlang.org/u/Mike_Wilkes)\
**Post date:** [June 6, 2018, 11:20am UTC](https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971/4 "2018-06-06T11:20:30Z")

</div>

Kotlin already has `isNullOrEmpty` on String? so no need to write a function yourself.

---

<div class="post-metadata">

**Author:** ![Wasabi375](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wasabi375/32/4741_2.png) [@Wasabi375](https://discuss.kotlinlang.org/u/Wasabi375)\
**Post date:** [June 6, 2018, 11:25am UTC](https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971/5 "2018-06-06T11:25:42Z")

</div>

Woops wasn’t thinking about this function 😉 I was in explanation mode and not thinking about the best solution but more about how to explain the mistake @silwar made.  
But yeah using `isNullOrEmpty` and inverting it is definitely a better solution.

---

<div class="post-metadata">

**Author:** ![Mike\_Wilkes](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/mike_wilkes/32/2620_2.png) [@Mike\_Wilkes](https://discuss.kotlinlang.org/u/Mike_Wilkes)\
**Post date:** [June 6, 2018, 11:44am UTC](https://discuss.kotlinlang.org/t/isnotempty-method-returns-null-on-nullable-string-object/7971/6 "2018-06-06T11:44:04Z")

</div>

You did a great job explaining this Kotlin behaviour, so I’m sure others will benefit from the explanation.

But yes, we want the easiest solution too 😉 All part of being a community. We watch over each other…
