1

I am writing an extension for Micro:Bit that controls an OLED. I have an array that I declare outside of any function. When I try to use it inside of a function I get the error "Program Error: Dereferencing Null/Undefined Value" in an orange pop-up.

As far as I can tell, the program does not recognize the array name inside of any function, and I'm able to create a new array with that name as if it were out of scope. Running the same code directly below the initial declaration (outside of any function) behaves as expected

Below is the simplest version I could make that still produces the error:

//% color="#00CC99"
namespace OLED_Test {

    let screenBuffer = [0x00]

    //block
    export function init() {
        let x = screenBuffer[0]
    }
}

1 Answers1

0

So, I still don't know why it doesn't work, but I discovered that if I declare the variable in the namespace and then set it inside of an init function, the variable will be have appropriately for the rest of the code. As such I just put all of my initial variable assignments inside of my init function, e.g:

//% color="#00CC99"
namespace OLED_Test {

    let screenBuffer: Array<number>

    //block
    export function init() {
        screenBuffer = [0x00]
    }
    export function foo(){
        let x = screenBuffer[0]
    }
}
  • Hey! I experienced the same indescriptive error message and it also turned out something to do with initializing arrays EXTRA carefully, the microbit typescript compiler is very picky to that. – raveren Oct 19 '19 at 02:54