# NRE from an "uninitialized this" in constructor

**URL:** <https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966>\
**Category:** Support\
**Created:** [September 12, 2016, 10:13pm UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966 "2016-09-12T22:13:56Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![jhoak](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jhoak](https://discuss.kotlinlang.org/u/jhoak)\
**Post date:** [September 12, 2016, 10:13pm UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/1 "2016-09-12T22:13:56Z")

</div>

Hi there,

I’m relatively new to Kotlin, and its handling of nulls / prevention of NREs looks really cool. But I’m reading the section that describes the four specific scenarios where one can still get NREs, and I’m having trouble visualizing the last one:

“There’s some data inconsistency with regard to initialization (an uninitialized this available in a constructor is used somewhere)”

When would this happen?

---

<div class="post-metadata">

**Author:** ![yole](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/yole/32/1425_2.png) [@yole](https://discuss.kotlinlang.org/u/yole)\
**Post date:** [September 13, 2016, 9:25am UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/2 "2016-09-13T09:25:54Z")

</div>

```auto
class Foo {
    val c: String

    init {
        bar()
        c = ""
    }

    fun bar() {
        println(c.length)
    }
}

```

---

<div class="post-metadata">

**Author:** ![jhoak](https://avatars.discourse-cdn.com/v4/letter/j/47e85d/32.png) [@jhoak](https://discuss.kotlinlang.org/u/jhoak)\
**Post date:** [September 14, 2016, 11:12pm UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/3 "2016-09-14T23:12:31Z")

</div>

Ah, of course! Thank you for the example.

---

<div class="post-metadata">

**Author:** ![elifarley](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elifarley/32/1804_2.png) [@elifarley](https://discuss.kotlinlang.org/u/elifarley)\
**Post date:** [November 8, 2019, 4:14pm UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/4 "2019-11-08T16:14:45Z")

</div>

Is this a compiler bug or a conscious design decision?

The compiler is smart enough when we inline the `bar` function:

```
class Foo {
    val c: String

    init {
        // bar()
        println(c.length) // Compile-time error: Variable 'c' must be initialized
        c = ""
    }

    fun bar() {
        println(c.length)
    }

}

```

So why doesn’t the compiler check for uninitialized variables inside `bar()` ? Would it be too time-consuming?

---

<div class="post-metadata">

**Author:** ![al3c](https://avatars.discourse-cdn.com/v4/letter/a/e47774/32.png) [@al3c](https://discuss.kotlinlang.org/u/al3c)\
**Post date:** [November 8, 2019, 5:43pm UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/5 "2019-11-08T17:43:35Z")

</div>

In the general case you can’t tell whether that is safe or not unfortunately.

If the code is inlined then as you say the compiler knows and tells you.

However if you call a method and that method gets overridden in a subclass you can’t really figure out if an access is safe or not.  
This is because the 2 classes are compiled independently at different times.

---

<div class="post-metadata">

**Author:** ![elifarley](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/elifarley/32/1804_2.png) [@elifarley](https://discuss.kotlinlang.org/u/elifarley)\
**Post date:** [November 9, 2019, 9:41am UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/6 "2019-11-09T09:41:41Z")

</div>

> However if you call a method and that method gets overridden in a **subclass** you can’t really figure out if an access is safe or not.

Class `Foo` cannot be subclassed as it isn’t open, so I don’t think your explanation applies to this case.

Here’s a modified version of that class, in which the NPE still happens:

```
private class Foo {
    private val c: String
    init {
        bar()
        c = ""
    }
    private inline fun bar() = c.length
}

```

---

<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:** [November 9, 2019, 2:48pm UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/7 "2019-11-09T14:48:27Z")

</div>

Even with a final class it’s not always possible for the compiler to check all paths. You can have if-statements, call other functions, pass the `this` instance around, etc. Unless you want to restrict `init` to just assignments there are situtations where the compile won’t be able to check for NPEs.  
The kotlin compiler therefor will only check the simplest situations and this means it won’t check functions called from `init`. Yes it is possible to write a better check for this, but this comes with performance issues and is quite complex. Maybe a stronger check can be added to kotlin at a later point but right now it does not exist and as far as I know is not planed. Also a full check won’t ever be possible unless all classes are final and compiled at the same time (which even if they are final they are not).  
So to answer your earlier question, this is by design and not a compiler bug.

---

<div class="post-metadata">

**Author:** ![Dmitriy.Novozhilov](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/dmitriy.novozhilov/32/6818_2.png) [@Dmitriy.Novozhilov](https://discuss.kotlinlang.org/u/Dmitriy.Novozhilov)\
**Post date:** [November 10, 2019, 10:25am UTC](https://discuss.kotlinlang.org/t/nre-from-an-uninitialized-this-in-constructor/1966/8 "2019-11-10T10:25:46Z")

</div>

1. Where compiler should report error, on call `bar()` on inside body of bar at `c.length`?
2. Which function calls should be checked by compiler – only `private inline`? Actually there is no difference in that case between `private inline` and all others functions that have reference to leaking `this` (member or not)
3. What if `bar` calls some other member function, that possibly access uninitialized `c`? Should compiler checks it’s body too?

Actually, as @Wasabi375 described, it’s possible to add such “cross-function” analysis, but it will lead to dramatically increasing of compilation time, because of each function call can run separate analysis of whole project, so it won’t be implemented in compiler. Current version of analysis works with _O(LOC)_ asymptotic
