Math.mean() Crash

Hello.

I am trying to get Math.mean() to work but it keeps crashing.

The following code works fine:

var untypedArray = new [0];
untypedArray.add(0);
untypedArray.add(1);
untypedArray.add(2);
var mean = Math.mean(untypedArray);

The following code crashes with: Error: Invalid Value Details: Cannot convert object to Double
var hr_array = new Array<Number>[10];//this is filled elsewhere in the program like this: hr_array[9] = info.heartRate;
var array = new Array<Number>[0];
for(index = 0; index < 10; index++) {
    array.add(hr_array[index]);
}
var mean = Math.mean(array);//crash here -  Cannot convert object to Double
I have tried the following with no luck:
var array = new Array<Number>[0];
var array = new [0];
var array = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
Thank you
  • What's info.heartRate? Is it Toybox.ActivityMonitor.HeartRateSample.heartRate?

    If so, it can be either Lang.Number or null. If you're not checking whether info.heartRate is null before adding it to the array, that's probably what's causing the crash. (Math.mean() expects an array of non-null Lang.Numeric elements.)

    This is where strict type checking would be helpful in preventing crashes.

    I can cause the same crash with the following code:

    var x = new Array[1];
    x[0] = null;
    Math.mean(x);

    Output:

    Error: Invalid Value
    Details: Cannot convert object to Double

  • Thanks for the response. I didn't post the code because it is long and complex and I was trying to make the question simple to understand. The hearRate is coming from Sensor.Info.heartRate. I can confirm that the values are not null. If there is no sensor reading, then it is set to zero.

    Also, this crashes as well:

    var untypedArray = new [10];
    for(index = 0; index < 10- 1; index++) {
         untypedArray[index] = 0;
    }

     var mean = Math.mean(untypedArray);
  • The hearRate is coming from Sensor.Info.heartRate. I can confirm that the values are not null. If there is no sensor reading, then it is set to zero.

    Sensor.Info.heartRate is typed as Lang.Number or Null.

    Are you absolutely sure that when your app crashes, it's not due to calling Math.mean() on an array that contains a null value?

    I don't see any other plausible explanation.

  • Error: Invalid Value Details: Cannot convert object to Double Stack: - calculateHrMF() at C:\Projects\Garmin\GarminSensorTest\GarminSensorTest\source\Cssi.mc:57 0x100001de

    and line 57 is: 

    var mean = Math.mean(array);
  • Thanks but that doesn't tell us what the contents of the array are.

    You said:

    > //this is filled elsewhere in the program like this: hr_array[9] = info.heartRate;

    and

    > The hearRate is coming from Sensor.Info.heartRate

    The docs say that Sensor.Info.heartRate can be null. Since you didn't mention anything about doing a null check in your code before assigning hr_array[9] = info.heartRate, my best guess is that info.heartRate is in fact sometimes null, which causes Math.mean() to crash.

    I don't know if this is crashing in the sim or device, but there's many ways you could verify my theory.

    1) Add a null check before assigning info.heartRate to hr_array[9] and see if the crash goes away

    2) If running in the sim, use System.println() to log the value of info.heartRate before assigning it. (Combine with 1. so your app doesn't crash)

    3) If running in the device, change something on the screen if info.heartRate is ever null.

  • Thank you for helping me out with this so far. I understand that I did not post all of the code and you have no way of making sure that I did not make a mistake somewhere else. I know for a fact that the value is not null but let's forget about any sensors or outside code. Let's try to simplify the issue and the question.

    This crashes in the simulator:

    var untypedArray = new [VITAL_BUF_SIZE];
            for(index = 0; index < VITAL_BUF_SIZE - 1; index++) {
                untypedArray[index] = 0;
            }
            var test = Math.mean(untypedArray);//crash here on line 55 
    Error: Invalid Value Details: Cannot convert object to Double Stack: - calculateHrMF() at C:\Projects\Garmin\GarminSensorTest\GarminSensorTest\source\Cssi.mc:55 0x100001f1
  • Thank you for helping me out with this so far.

    No worries!

    This crashes in the simulator:

    That's because the final element of untypedArray is null.

    var untypedArray = new [VITAL_BUF_SIZE]; // creates an array of VITAL_BUF_SIZE elements, all initialized to null

    for(index = 0; index < VITAL_BUF_SIZE - 1; index++) { // iterates from 0 to VITAL_BUF_SIZE - 2 (inclusive)

    Change your for loop as follows and it won't crash:

    for (index = 0; index < VITAL_BUF_SIZE; index++) {

    Again, in your real code (based on Sensor.info.heartRate), it's easy enough to prove or disprove my theory. Or just enable type checking (which will point out the same thing I did.)

  • Your theory was correct and the code is now working. Thank you very much for your assistance!