# Strange problem in kotlin

**URL:** <https://discuss.kotlinlang.org/t/strange-problem-in-kotlin/8616>\
**Category:** Android\
**Created:** [July 17, 2018, 9:22pm UTC](https://discuss.kotlinlang.org/t/strange-problem-in-kotlin/8616 "2018-07-17T21:22:47Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![roiamiel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/roiamiel/32/5359_2.png) [@roiamiel](https://discuss.kotlinlang.org/u/roiamiel)\
**Post date:** [July 17, 2018, 9:22pm UTC](https://discuss.kotlinlang.org/t/strange-problem-in-kotlin/8616/1 "2018-07-17T21:22:47Z")

</div>

Hey,  
I’m building Android app right now, and I found a strange problem occurred in kotlin 1.2.51:

```
Handler(Looper.getMainLooper()).post {
        for (line in lines) {
            // do stuff
        }

        // do smothing else
    }

```

when I’m running this code (`lines` is an empty mutable list) the program crashing.  
In the log, it says that it crashed a few lines before that.

But the strange thing is that when I removed the `for (line in lines)` loop ( **loop over empty list!!** ), it runs just fine.

```
2018-07-18 00:13:40.660 16387-17204/com.my.app E/AndroidRuntime: FATAL EXCEPTION: Thread-28
Process: com.my.app, PID: 16387
kotlin.KotlinNullPointerException
    at com.my.app.fragments.SomeFragment$onCreate$2.run(SomeFragment.kt:102)
    at java.lang.Thread.run(Thread.java:764)

```

This is the java decompiled:

```
(new Handler(Looper.getMainLooper())).post((Runnable)(new Runnable() {
    public final void run() {
        Iterator var2 = lines.iterator();

        while(var2.hasNext()) {
            // do stuff
        }

        // do smothing else
}));

```

How can I manage to solve this problem? or at least find out why this is happening…

Thanks.

---

<div class="post-metadata">

**Author:** ![darksnake](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/darksnake/32/2479_2.png) [@darksnake](https://discuss.kotlinlang.org/u/darksnake)\
**Post date:** [July 18, 2018, 5:22am UTC](https://discuss.kotlinlang.org/t/strange-problem-in-kotlin/8616/2 "2018-07-18T05:22:56Z")

</div>

An interesting case, but maybe it have a simple solution. You have `KotlinNullPointExcrption` which is raised when something coming from java is not supposed to be null, but null nevertheless. I think that `lines` object you are using is no just empty, but null. It will explain the behavior.

---

<div class="post-metadata">

**Author:** ![roiamiel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/roiamiel/32/5359_2.png) [@roiamiel](https://discuss.kotlinlang.org/u/roiamiel)\
**Post date:** [July 22, 2018, 5:11pm UTC](https://discuss.kotlinlang.org/t/strange-problem-in-kotlin/8616/3 "2018-07-22T17:11:40Z")

</div>

Hey @darksnake,

`lines` is a non-nullable, but I somehow managed to solve this problem

Thanks anyway.
