Calling length on an array triggers null errors on random parts of the code...

Former Member
Former Member

Hello, this code works fine:

    hidden function renderCalendarEvents (dc) {
        var events = Storage.getValue("events");
        if(events == null){return;}
        renderCalendarLine(dc,events[0],0);
        renderCalendarLine(dc,events[1],1);
    }

If I add a single line like this:

    hidden function renderCalendarEvents (dc) {
        var events = Storage.getValue("events");
        if(events == null){return;}
        renderCalendarLine(dc,events[0],0);
        renderCalendarLine(dc,events[1],1);
        System.println(events.length());
    }

I get a null error on the function that I was calling:

Error: Unhandled Exception
Exception: UnexpectedTypeException: Expected Number/Boolean/Long, given null
Stack: 
  - renderCalendarEvents() at /Users/danielo/eclipse-workspace-java/garmin-hello-world/source/garminhelloworldView.mc:92 0x100012ae 
  - onUpdate() at /Users/danielo/eclipse-workspace-java/garmin-hello-world/source/garminhelloworldView.mc:181 0x10001823 

No matter where I add the call to events.length, I always get the same error.

What is this?