Strange problem in kotlin

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.

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.

Hey @darksnake,

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

Thanks anyway.