# Program error: sim error: failed cast on null

**URL:** <https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831>\
**Category:** Help\
**Created:** [May 20, 2024, 1:25am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831 "2024-05-20T01:25:22Z")\
**Posts on this page:** 12\
**Page:** 1

<div class="post-metadata">

**Author:** ![ruprion](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ruprion/32/15723_2.png) [@ruprion](https://forum.makecode.com/u/ruprion)\
**Post date:** [May 20, 2024, 1:25am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/1 "2024-05-20T01:25:22Z")

</div>

I get the error “failed cast on null” on my project whenever i press A. My project is big so i cant find out what’s the problem with it. I need to finish this project for school so please help

Here is the project: [https://arcade.makecode.com/S49194-82501-20610-37465](https://arcade.makecode.com/S49194-82501-20610-37465)

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [May 20, 2024, 3:16am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/2 "2024-05-20T03:16:15Z")

</div>

Hi, @ruprion !

Whenever I get an error like this, especially when the error is _repeatable_ (meaning, I know when the error will happen), then I often use the debugger to show me where the problem is.

To turn on the debugger, select the **debug** button below the game controls. Screenshot below.

![Debug button highlighted beneath the MakeCode Arcade simulator](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/0/0b3fafd8735511a1439f2f10f10a8c56376852f3.png)

When I run the debugger on your program and select the **A** button after the attract screen, the debugger highlights this block inside of your **on A button pressed** handler:

![Block with error highlighted in debugger](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/537fbfabb75b51b9a5a825743e9c4243de68caab.png)

I’m guessing that the variable `mySprite` has not yet been defined when you exit the attract screen. You may need to create a new variable (perhaps something like `isGameStarted`) that you set to `true` once the game actually begins, and then check that variable within your **A** button handler:

![Original on A button pressed handler now surrounded by an block](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/9/9e789fce54ed9e2f348e2fbb18e408070a143e39.png)

Let us know if you’re still stuck! Good luck on your project!

---

<div class="post-metadata">

**Author:** ![ruprion](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ruprion/32/15723_2.png) [@ruprion](https://forum.makecode.com/u/ruprion)\
**Post date:** [May 20, 2024, 3:19am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/3 "2024-05-20T03:19:16Z")

</div>

Okay, I’ll try this. Thanks for the advice!

---

<div class="post-metadata">

**Author:** ![ruprion](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ruprion/32/15723_2.png) [@ruprion](https://forum.makecode.com/u/ruprion)\
**Post date:** [May 20, 2024, 3:49am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/4 "2024-05-20T03:49:36Z")

</div>

Okay, so now I’m trying to figure out how to set the variable to true. Can you help me out?

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [May 20, 2024, 3:51am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/5 "2024-05-20T03:51:31Z")

</div>

They’re at the bottom of the **Logic** drawer. You can set variables to `true` and `false` with the same control. Just use the dropdown to change the value.

 ![Bottom of the Logic drawer showing the and blocks](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/5/573a9a02a199c23988e7f9d31975a6ba8a4ec6f8.png)

So, you could do something like this at the beginning of your `on start`:

![using the block](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/2/2a8dcc1a3b872068f37f9abf0f330cd29b2ee18f.png)

Then, wherever it’s appropriate in your code, set it to `true`.

---

<div class="post-metadata">

**Author:** ![ruprion](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ruprion/32/15723_2.png) [@ruprion](https://forum.makecode.com/u/ruprion)\
**Post date:** [May 20, 2024, 4:39am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/6 "2024-05-20T04:39:40Z")

</div>

Okay, so this is the code so far, It still doesn’t work. Let me know how to fix it.

---

<div class="post-metadata">

**Author:** ![ruprion](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/ruprion/32/15723_2.png) [@ruprion](https://forum.makecode.com/u/ruprion)\
**Post date:** [May 20, 2024, 4:39am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/7 "2024-05-20T04:39:47Z")

</div>

![image_2024-05-19_213523512](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/d/dd66a1b5b94a2105694bd4025a4983ba3d5e1c77.png)

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [May 20, 2024, 4:50am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/8 "2024-05-20T04:50:16Z")

</div>

I think I know what the problem is, but just to be sure I’m answering the right question: What “still doesn’t work?” What are you expecting your program to do, and how is it behaving instead?

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [May 20, 2024, 5:25am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/9 "2024-05-20T05:25:11Z")

</div>

If it’s the question that I think you’re asking, then try this: Add a `game.splash` block right after your call to `Intro`:

![image](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/b/bebaa19b4416d2efd51526da97d54917cc7a7e6b.png)

If I’m interpreting your code correctly, then you’re expecting to be able to press the **A** button after your intro screen appears. If this is what you want your code to do, then a great test is to place a `game.splash` block here as I’ve done.

Splash blocks are great. They kinda “pause” your game to make sure that the code that you are expecting to run is actually running.

Now … run your code. You’ll see that the splash block never runs.

To find out why, it’s time to use one of my favorite tools in MakeCode: **slow-mo mode**. Activate the debug mode as mentioned in a previous post. Once there, select the snail to enter slow-mo mode. Then, watch the highlight as slow-mo mode follows your code.

 ![Slow-mo button (the snail button) highlighted in MakeCode Arcade debug mode](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/2X/a/adaa12d7ff4210875a3894c691e2733629572037.png)

You’ll find that, once you enter your **Intro** function, it never returns. Actually, if you run your program for long enough, you will encounter an **out of memory** error. I’d suggest taking advantage of your new variable, `isGameRunning`, to decide whether to play melodies or to return from your functions.

---

<div class="post-metadata">

**Author:** ![pecan4pi](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/pecan4pi/32/20940_2.png) [@pecan4pi](https://forum.makecode.com/u/pecan4pi)\
**Post date:** [May 20, 2024, 11:49pm UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/10 "2024-05-20T23:49:55Z")

</div>

Woah, this is a basically whole tutorial on debug mode! I know I wasn’t the one who called for help, but this helped me anyways! I always wondered what the snail button did, but my brain was to small to put the two together. 😅  
Also I have a question, can you toggle slow motion on and off while playing in debug mode? Say you have one crash that’s not at the start of the game and you want the simulator to to go into slo-mo to see what exactly happens (without spending 20 minutes for everything to go frame by frame through all the calculations). Can you do that? If you can’t, could that possibly be a feature added in a later version of Makecode? (if not that’s totally fine, I know you guys have a long to-do list.) Thanks!

---

<div class="post-metadata">

**Author:** ![richard](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/richard/32/5417_2.png) [@richard](https://forum.makecode.com/u/richard)\
**Post date:** [May 20, 2024, 11:50pm UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/11 "2024-05-20T23:50:30Z")

</div>

@pecan4pi not supported today, but totally reasonable request! could you file an issue at [github.com/microsoft/pxt-arcade](http://github.com/microsoft/pxt-arcade)?

---

<div class="post-metadata">

**Author:** ![AlexK](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/alexk/32/33339_2.png) [@AlexK](https://forum.makecode.com/u/AlexK)\
**Post date:** [May 21, 2024, 1:30am UTC](https://forum.makecode.com/t/program-error-sim-error-failed-cast-on-null/28831/12 "2024-05-21T01:30:42Z")

</div>

Thanks, @pecan4pi ! There’s a bit more to the debugger, and I hadn’t thought of doing a proper tutorial here on that topic … but you’ve convinced me! 😁 First installment in the tutorial (and probably the second, too) will drop this week. Stay tuned!
