# Page 394 problem! fatal error in pest control! PLEASE HELP

**URL:** <https://forums.kodeco.com/t/page-394-problem-fatal-error-in-pest-control-please-help/26836>\
**Category:** 2D iOS & tvOS Games by Tutorials\
**Created:** [March 3, 2017, 10:48am UTC](https://forums.kodeco.com/t/page-394-problem-fatal-error-in-pest-control-please-help/26836 "2017-03-03T10:48:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![joeeydubu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/joeeydubu/32/61365_2.png) [@joeeydubu](https://forums.kodeco.com/u/joeeydubu)\
**Post date:** [March 3, 2017, 10:48am UTC](https://forums.kodeco.com/t/page-394-problem-fatal-error-in-pest-control-please-help/26836/1 "2017-03-03T10:48:55Z")

</div>

okay so I got through the whole of pest control and finally came to the end 394, and added all of the init(coder:) to the player, bug, game scene, then I go to run and it fatal errors saying unexpectedly found nil whilst unwrapping an optional on this line

“animations = aDecoder.decodeObject(forKey: “Player.animations”) as! [SKAction]”

I even tried to just run the final source file for this chapter and I still get the same error, so it can’t be me? it must be something wrong with the book? I have had this EXACT same error with catNap twice, where I have even used the source final files which is provided by raywenderlich and I get the same errors? what is going on PLEASE HELP

` required init?(coder aDecoder: NSCoder) { super.init(coder: aDecoder) animations = aDecoder.decodeObject(forKey: "Player.animations") as! [SKAction] hasBugspray = aDecoder.decodeBool(forKey: "Player.hasBugspray") if hasBugspray { removeAction(forKey: "blink") blink(color: .green, on: hasBugspray) } }`

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [March 3, 2017, 12:38pm UTC](https://forums.kodeco.com/t/page-394-problem-fatal-error-in-pest-control-please-help/26836/2 "2017-03-03T12:38:13Z")

</div>

@joeeydubu - I’m sorry to hear that you’re having trouble running the code.

I just ran the project in the `final` folder in Chapter 14, and it runs without error. Are you having trouble running that project?

I can get the error that you’re describing if I don’t encode `animations`. In that case, the decoder is looking for something that doesn’t exist in the saved archive file.

If you open the final project, in Player.swift on line 118, there’s an `encode(with:)` method which encodes `animations`. Do you have that in the project where you’re having an error?

It might also be that you have a bad archive file saved in the Documents folder. Sometimes during development, you can get an archive file from a previous development phase. Try removing the project from the simulator.

(Thinking about it, it’s more likely that you have a bad file. You can also go to the simulator folder as described in the book and delete that archive file.)

---

<div class="post-metadata">

**Author:** ![joeeydubu](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/joeeydubu/32/61365_2.png) [@joeeydubu](https://forums.kodeco.com/u/joeeydubu)\
**Post date:** [March 5, 2017, 7:41am UTC](https://forums.kodeco.com/t/page-394-problem-fatal-error-in-pest-control-please-help/26836/3 "2017-03-05T07:41:47Z")

</div>

oh my thank you so much!! that was such an easy fix!  
but now I am wondering, how would I have known to do that for the future of when I start making my own games? like it says no optional when unwrapping for animations, but if it was something to do with the archive file, how was I suppose to know that?

thankyou so much for your help!! (::

---

<div class="post-metadata">

**Author:** ![caroline](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/caroline/32/111867_2.png) [@caroline](https://forums.kodeco.com/u/caroline)\
**Post date:** [March 5, 2017, 10:01am UTC](https://forums.kodeco.com/t/page-394-problem-fatal-error-in-pest-control-please-help/26836/4 "2017-03-05T10:01:26Z")

</div>

@joeeydubu - that’s a very good question. I think when I was writing the chapter, I scratched my head a bit when I first got that error.

However, when you see a `!` in code, think `DANGER!`

Because we (authors) have limited space and it’s also important to have brevity to get the concept across, we often use the `!` (implicitly unwrapped optional) as a shortcut to always checking that something is there.

If I were doing full error checking, I would write something like this:

```auto
if let animations = aDecoder.decodeObject(forKey: "Player.animations") as? [SKAction] {
 self.animations = animations
} else {
  fatalError("animations were not found in the archive file")
}

```

The error is an obvious one - you’re looking for animations in a coded file, and if you don’t find one and it should be there, and it’s an error that should crash the app, put out a crash error that it’s not there.

Your app would still have crashed, but it would have crashed with the message `"animations were not found in the archive file"`, and may have prompted you to think about why they weren’t in the archive file.

I would recommend not shipping with a `!` anywhere in your code. Except for `@IBOutlets` and except for times when you are really really really sure that the property will have something other than nil in it **every** time you access it.

I also use `print` a lot. When I get crashes, I print out contents of properties every time I set or access them and make sure that what’s being printed makes sense.

---

<div class="post-metadata">

**Author:** ![nehemie](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@nehemie](https://forums.kodeco.com/u/nehemie)\
**Post date:** [May 14, 2017, 2:35pm UTC](https://forums.kodeco.com/t/page-394-problem-fatal-error-in-pest-control-please-help/26836/5 "2017-05-14T14:35:33Z")

</div>

I had the same problem and you saved me from a headache

thanks Caroline
