# 🪲"Sim Error: Failed Cast on Null" Explained

**URL:** <https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926>\
**Category:** Help\
**Tags:** tutorial\
**Created:** [October 22, 2025, 9:26pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926 "2025-10-22T21:26:48Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![MeatballDev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/meatballdev/32/28502_2.png) [@MeatballDev](https://forum.makecode.com/u/MeatballDev)\
**Post date:** [October 22, 2025, 9:26pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/1 "2025-10-22T21:26:48Z")

</div>

# “Sim Error: Failed Cast on Null” Explained

![Screenshot 2025-10-21 9.12.12 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/2/6/26380e388f94bcb2b899e8cd13ed800f26c6320d.png)

I’ve seen so many posts about this bug and its the easiest one to fix so I’m going to explain the bug and how to fix it

### **What is Failed Cast on Null**

Failed Cast on Null is an Error in Makecode. This error occurs when in the MakeCode editor simulator when it is trying to process the program and there is an error in the code. This is usually caused by an Undeclared Variable,\[1\] Race Conditions,\[2\] and Incorrect Object Reference.\[3\]

### **Example**

**Micro-Bit**  
In a program using the **Neopixel** extension, a **Set Brightness** block is added in the wrong order.

 ![k7kf1QbyeIsxHmeaCYaHVJGcJ49VLUEbkA (1)](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/7/a/7ac9d81d0bdafd5309889b20c555c09434dc314e.png)  
The variable **strip** is null in the **Set Brightness** block, which is more obvious in JavaScript mode.  
 ![DpgEk8I85lwejRCXnCrUTin32a8BeVeGdg](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/3/1/31072af153560e21311a75e102c14474fb922c8d.png)

**Arcade**  
In a simple program where a sprite changes location, a **Set X** block is added in the wrong order.

 ![Screenshot 2025-10-21 9.38.03 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/b/6/b6da6fc76424cda093649822dcce7147da0c9708.png)  
The variable **mySprite** is null in the **Set Brightness** block, which is once again more obvious in JavaScript mode.  
 ![Screenshot 2025-10-21 9.37.52 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/7/d/7df55ca860a59f5341138221ad94cdcdba9a6875.png)

### **Locating the Bug**

In order to locate the bug you will have to do the following

1. Enter Debug Mode \[4\]
2. Press the Play Button \[5\]
3. You will be sent to the source of the bug

### **Solutions and Fixes**

- **Ensure proper initialization order:** Place the code that creates or initializes an object before any code that modifies it. For example, put the “set sprite” block before any “move sprite” blocks.

- **Add delays or flags:** If an object is created asynchronously, use a delay or a boolean flag to ensure the code that needs it doesn’t run until the object is ready.

* * *

* * *

1. Google Gemini: A variable is used before it has been declared and assigned a value. 

2. Google Gemini: Code is trying to use an object before it has been fully created or loaded, which can happen if a sprite creation is dependent on user input or a delay. 

3. Google Gemini: A script or function is receiving a null value instead of the expected object, often from a database or a previous function call that failed. 

4.   

5.   
 ![Screenshot 2025-10-21 9.52.31 AM](https://us1.discourse-cdn.com/flex020/uploads/makecode/original/3X/5/c/5ce6ad5fec71eb2a969dfebc9cee8e50e3842500.png)

---

<div class="post-metadata">

**Author:** ![MyCraft\_Steve](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mycraft_steve/32/25707_2.png) [@MyCraft\_Steve](https://forum.makecode.com/u/MyCraft_Steve)\
**Post date:** [October 23, 2025, 4:48am UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/2 "2025-10-23T04:48:18Z")

</div>

I saw the orange alert and nearly got a heart attack, please don’t scare me like that

---

<div class="post-metadata">

**Author:** ![Mr.SonicFan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mr.sonicfan/32/22384_2.png) [@Mr.SonicFan](https://forum.makecode.com/u/Mr.SonicFan)\
**Post date:** [October 23, 2025, 4:36pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/3 "2025-10-23T16:36:12Z")

</div>

Did you use AI? Because i’ve literally never heard of a “boolean flag” in Microsoft Makecode and a few other things

---

<div class="post-metadata">

**Author:** ![RoboVacDev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/robovacdev/32/29619_2.png) [@RoboVacDev](https://forum.makecode.com/u/RoboVacDev)\
**Post date:** [October 23, 2025, 4:38pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/4 "2025-10-23T16:38:42Z")

</div>

This is really helpful, thanks. But what do you mean by boolean flag and delay?

---

<div class="post-metadata">

**Author:** ![MeatballDev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/meatballdev/32/28502_2.png) [@MeatballDev](https://forum.makecode.com/u/MeatballDev)\
**Post date:** [October 23, 2025, 4:43pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/5 "2025-10-23T16:43:29Z")

</div>

I was worn out in the end and used ai but forgot to mention it

---

<div class="post-metadata">

**Author:** ![MeatballDev](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/meatballdev/32/28502_2.png) [@MeatballDev](https://forum.makecode.com/u/MeatballDev)\
**Post date:** [October 23, 2025, 4:43pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/6 "2025-10-23T16:43:54Z")

</div>

Did this Help

_Poll ([view on site](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/6))_

---

<div class="post-metadata">

**Author:** ![emory55](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/emory55/32/17766_2.png) [@emory55](https://forum.makecode.com/u/emory55)\
**Post date:** [October 23, 2025, 5:39pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/7 "2025-10-23T17:39:13Z")

</div>

Boolean is True or False variable

> **[Boolean](https://arcade.makecode.com/blocks/logic/boolean)**
>
> A Boolean has one of two possible values: true or false. Boolean (logical) operators (and, or, not) take Boolean inputs and make another Boolean value. Comparison operators on other types (numbers, strings) create Boolean values.

I personally just use a standard variable and check if it is 0 or 1. The advantage to using numbers is they allow for multiple state cases (Like Game\_state, Pause\_state, Title\_screen\_state, etc).

---

<div class="post-metadata">

**Author:** ![Mr.SonicFan](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mr.sonicfan/32/22384_2.png) [@Mr.SonicFan](https://forum.makecode.com/u/Mr.SonicFan)\
**Post date:** [October 23, 2025, 11:38pm UTC](https://forum.makecode.com/t/sim-error-failed-cast-on-null-explained/39926/8 "2025-10-23T23:38:38Z")

</div>

It’s fine, it still helps alot 🙂 👏
