🪲"Sim Error: Failed Cast on Null" Explained

“Sim Error: Failed Cast on Null” Explained

Screenshot 2025-10-21 9.12.12 AM

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.


The variable strip is null in the Set Brightness block, which is more obvious in JavaScript mode.
DpgEk8I85lwejRCXnCrUTin32a8BeVeGdg

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


The variable mySprite is null in the Set Brightness block, which is once again more obvious in JavaScript mode.

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
    ↩︎

3 Likes

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

3 Likes

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

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

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

Did this Help

  • Yes
  • No
0 voters

Boolean is True or False variable

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).

1 Like

It’s fine, it still helps alot :slightly_smiling_face: :clap: