# Enum initialization method gives NullPointerException

**URL:** <https://discuss.kotlinlang.org/t/enum-initialization-method-gives-nullpointerexception/2518>\
**Category:** Support\
**Created:** [April 23, 2017, 12:57pm UTC](https://discuss.kotlinlang.org/t/enum-initialization-method-gives-nullpointerexception/2518 "2017-04-23T12:57:18Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![wouterdoeland](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wouterdoeland/32/2475_2.png) [@wouterdoeland](https://discuss.kotlinlang.org/u/wouterdoeland)\
**Post date:** [April 23, 2017, 12:57pm UTC](https://discuss.kotlinlang.org/t/enum-initialization-method-gives-nullpointerexception/2518/1 "2017-04-23T12:57:19Z")

</div>

I have a few Enums with some Strings for creating MySQL tables. When I try to access one, it throws a NullPointerException.  
See this screenshot:

 ![](https://us1.discourse-cdn.com/flex019/uploads/kotlinlang/original/2X/4/4114c711320fe5ba51a99942e12653eabefb26f5.png)  
Does anybody know why this is happening?

---

<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:** [April 23, 2017, 1:26pm UTC](https://discuss.kotlinlang.org/t/enum-initialization-method-gives-nullpointerexception/2518/2 "2017-04-23T13:26:06Z")

</div>

You’re accessing an enum constant from its constructor parameter. The constructor is executed during the creation of the object that is going to be assigned to the enum constant, and before that object is assigned to the field storing the enum constant. Therefore, accessing the `name` property of an uninitialized object gives an NPE.

---

<div class="post-metadata">

**Author:** ![wouterdoeland](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/wouterdoeland/32/2475_2.png) [@wouterdoeland](https://discuss.kotlinlang.org/u/wouterdoeland)\
**Post date:** [April 23, 2017, 1:33pm UTC](https://discuss.kotlinlang.org/t/enum-initialization-method-gives-nullpointerexception/2518/3 "2017-04-23T13:33:12Z")

</div>

Thanks, this works!  
I first thought this wasn’t the case because when I removed it in one, it still didn’t work. I had to remove it in all enums apparently.
