PROBLEM
My Forever loop starts before the OnStart has finished
I get runtime error 40 or 42, (NULL reference)
CAUSE
Under the covers, the micro:bit will start every Forever loop in it's own fiber (a fiber is a light-weight thread that can run independently from other fibers - a bit like a separate block of code in Scratch, for example). The On Start block will run to completion, then Forever blocks will start executing.
However, if there are any pause statements (or function usage that has an inherent pause facility) inside On Start, this will cause the fiber scheduler to start all other fibers, and your Forever loops will start before the On Start has completed.
This is normal operation.
WORKAROUND
Remove any statements in your On Start that pause.
Alternatively, if you need pauses in your On Start for some reason, at the end of your On Start set a boolean flag to True, and check the value of that flag in your Forever loops - only starting the Forever loop content once that boolean flag has been set to True.
FURTHER INFORMATION
fibers: https://lancaster-university.github.io/microbit-docs/concepts/#concurrency
INFO PROVIDED BY:
MS (https://github.com/Microsoft/pxt/issues/1324)
LAST REVIEWED
18 APR 2017 DW