# \[Screencast\] Handling Nil in Crystal

**URL:** <https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428>\
**Category:** Learning Resources\
**Created:** [February 27, 2022, 12:16am UTC](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428 "2022-02-27T00:16:36Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![stephendolan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stephendolan/32/950_2.png) [@stephendolan](https://forum.crystal-lang.org/u/stephendolan)\
**Post date:** [February 27, 2022, 12:16am UTC](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428/1 "2022-02-27T00:16:36Z")

</div>

First LuckyCast I’ve produced in a while, but it’s something that I remember hanging me up for quite a while when I first started learning Crystal. Hopefully, it’s helpful to some new folks getting started.

[![](https://img.youtube.com/vi/vMdFSRoY75Y/maxresdefault.jpg "Handling Nil in Crystal Lang") ](https://www.youtube.com/watch?v=vMdFSRoY75Y)

---

<div class="post-metadata">

**Author:** ![kees](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kees/32/914_2.png) [@kees](https://forum.crystal-lang.org/u/kees)\
**Post date:** [February 27, 2022, 10:01am UTC](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428/2 "2022-02-27T10:01:34Z")

</div>

@stephendolan what format is the video?  
I get the following error: “No video with supported format and MIME type found”

---

<div class="post-metadata">

**Author:** ![stephendolan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stephendolan/32/950_2.png) [@stephendolan](https://forum.crystal-lang.org/u/stephendolan)\
**Post date:** [February 27, 2022, 2:15pm UTC](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428/3 "2022-02-27T14:15:33Z")

</div>

I’ll take a look, @kees ! In the meantime, I’ve swapped out my link for the YouTube version 🙂

---

<div class="post-metadata">

**Author:** ![kees](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/kees/32/914_2.png) [@kees](https://forum.crystal-lang.org/u/kees)\
**Post date:** [February 28, 2022, 9:22am UTC](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428/4 "2022-02-28T09:22:04Z")

</div>

Thanks, that works for me!

---

<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:** [February 28, 2022, 12:28pm UTC](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428/5 "2022-02-28T12:28:12Z")

</div>

Nice video!

I think this is a recurring error that people trip into. That’s why we should probably put all this information into the compiler. It’s a bit tricky to determine how to do it, but here are some ideas:

- If there’s an “undefined method” error on an instance variable that can be nil, check if that instance variable is part of an `if` statement that encloses this use. If so, suggest assigning it to a local variable.
- Similarly, if it happens with a call, check if that call happens in an `if`. Or maybe in this case we could say “if you already checked whether `current_user` is nil or not before this line, make sure to first assign the result of that call to a local variable”

---

<div class="post-metadata">

**Author:** ![stephendolan](https://yyz2.discourse-cdn.com/flex036/user_avatar/forum.crystal-lang.org/stephendolan/32/950_2.png) [@stephendolan](https://forum.crystal-lang.org/u/stephendolan)\
**Post date:** [February 28, 2022, 6:48pm UTC](https://forum.crystal-lang.org/t/screencast-handling-nil-in-crystal/4428/6 "2022-02-28T18:48:13Z")

</div>

Thanks so much! In case it’s helpful reference material, here’s everything I could find on the topic on the forums and official GitHub repos while researching for the video:

- [Is casting the idiomatic way of dealing with nil?](https://forum.crystal-lang.org/t/is-casting-the-idiomatic-way-of-dealing-with-nil/1693)
- [Nil check for instance variables · Issue #4475 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/4475)
- [Immutable instance variables · Issue #4359 · crystal-lang/crystal · GitHub](https://github.com/crystal-lang/crystal/issues/4359)
