Program error: sim error: failed cast on null

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

2 Likes

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

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

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

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

3 Likes

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

2 Likes

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

1 Like

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.

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

using the  block

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

2 Likes

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

2 Likes

2 Likes

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?

2 Likes

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

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.

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.

2 Likes

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. :sweat_smile:
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!

2 Likes

@pecan4 not supported today, but totally reasonable request! could you file an issue at github.com/microsoft/pxt-arcade?

4 Likes

Thanks, @pecan4 ! 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! :grin: First installment in the tutorial (and probably the second, too) will drop this week. Stay tuned!

4 Likes