# Strange error message 'undefined method for Nil'

**URL:** <https://forum.crystal-lang.org/t/strange-error-message-undefined-method-for-nil/1130>\
**Category:** Help & Support\
**Created:** [September 19, 2019, 3:02pm UTC](https://forum.crystal-lang.org/t/strange-error-message-undefined-method-for-nil/1130 "2019-09-19T15:02:58Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Guillaume](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@Guillaume](https://forum.crystal-lang.org/u/Guillaume)\
**Post date:** [September 19, 2019, 3:02pm UTC](https://forum.crystal-lang.org/t/strange-error-message-undefined-method-for-nil/1130/1 "2019-09-19T15:02:58Z")

</div>

I have a compilation error when I try to use a class variable referencing itself:

```auto
(from class A)
 @b = B.new(self)
 @b.toto()

```

I have reproduced the error here: [https://play.crystal-lang.org/#/r/7lmf](https://play.crystal-lang.org/#/r/7lmf)  
This example works if b is not a class variable for instance. My question is why the compiler thinks @b can be null since I initialise it just before using it?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [September 19, 2019, 3:16pm UTC](https://forum.crystal-lang.org/t/strange-error-message-undefined-method-for-nil/1130/2 "2019-09-19T15:16:13Z")

</div>

Because you pass `self` to something else, and that something else could do anything, including reading `@b` which is not yet initialized.

You can do `@b.not_nil!.toto()` to workaround that.

---

<div class="post-metadata">

**Author:** ![Guillaume](https://avatars.discourse-cdn.com/v4/letter/g/c67d28/32.png) [@Guillaume](https://forum.crystal-lang.org/u/Guillaume)\
**Post date:** [September 19, 2019, 3:23pm UTC](https://forum.crystal-lang.org/t/strange-error-message-undefined-method-for-nil/1130/3 "2019-09-19T15:23:15Z")

</div>

Thank you for the workaround!

---

<div class="post-metadata">

**Author:** ![jgaskins](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/jgaskins/32/2449_2.png) [@jgaskins](https://forum.crystal-lang.org/u/jgaskins)\
**Post date:** [September 19, 2019, 3:56pm UTC](https://forum.crystal-lang.org/t/strange-error-message-undefined-method-for-nil/1130/4 "2019-09-19T15:56:14Z")

</div>

Actually, could it be because it isn’t defined in `initialize`?

---

<div class="post-metadata">

**Author:** ![asterite](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/asterite/32/60_2.png) [@asterite](https://forum.crystal-lang.org/u/asterite)\
**Post date:** [September 19, 2019, 4:20pm UTC](https://forum.crystal-lang.org/t/strange-error-message-undefined-method-for-nil/1130/5 "2019-09-19T16:20:15Z")

</div>

> [@jgaskins](#):
>
> Actually, could it be because it isn’t defined in `initialize` ?

Oh, yeah, that’s definitely it. I didn’t check the `play` link.
