# Failed cast on null error

**URL:** <https://forum.makecode.com/t/failed-cast-on-null-error/2575>\
**Category:** micro:bit\
**Created:** [June 29, 2020, 1:25pm UTC](https://forum.makecode.com/t/failed-cast-on-null-error/2575 "2020-06-29T13:25:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![ameyrr250](https://avatars.discourse-cdn.com/v4/letter/a/d9b06d/32.png) [@ameyrr250](https://forum.makecode.com/u/ameyrr250)\
**Post date:** [June 29, 2020, 1:25pm UTC](https://forum.makecode.com/t/failed-cast-on-null-error/2575/1 "2020-06-29T13:25:35Z")

</div>

Hi,  
I get the error “failed cast on null” for the code below. It only occurs on runtime. If I keep the export functions outside the class and make an object with  
let forceobj= new Force()  
I don’t get this error

Is there a way to keep functions that are displayed as blocks inside the class and make this work?

//\*\*  
//\* Provides access to basic micro:bit functionality.  
//\*/  
//% color=#1E90FF weight=116 icon="\uf00a"  
//% advanced=true

namespace Force\_{

```
//% block="create force settings"
//% blockSetVariable=someName
export function createForceSettings(): Force {
    return new Force();

```

}

```
export class Force{
   A : number;
   sumA : number;
   Force_voltage : number;
   Force_val : number;
   rangefactor : number;
   Vadc_3 : number;       

   constructor(){
   this.A=0;
   this.sumA=0;
   this.Force_voltage=0;
   this.Force_val=0;
   this.rangefactor=20/3.3
   this.Vadc_3=3.3/4096; 
   }

    /**
     * Measures force and returns string value.
     * @param BoardNum the board number at which connected
     */
    //% help=Force_/Force/forcestring
    //% block="Get string value at $this of force on $BoardNum"
    //% blockId=forceS
    //% blockNamespace=Force_
    //% this.shadow=variables_get
    //% this.defl=someName
    //% weight=90 blockGap=12 color=#9E4894 icon=""

    forcestring(BoardNum: BoardID) : string{
        let valueForce= this.force(BoardNum);
        let stringForce= valueForce.toString();
        return stringForce;
       }

    
    /**
     * Measures force and returns value.
     * @param BoardNum the board number at which connected
     */
    //% help=Force_/Force/forcestring
    //% block="Get value at $this of force on $BoardNum"
    //% blockId=force
    //% blockNamespace=Force_
    //% this.shadow=variables_get
    //% this.defl=someName
    //% weight=90 blockGap=12 color=#9E4894 icon=""

    force(BoardNum: BoardID) : number{
    for (let i=1;i<=20;i++)
    {
        this.A=(b1.analogRead(ADCPin.AN,BoardNum))
        this.sumA+=this.A;
    }
    this.sumA=this.sumA/20;
    this.Force_voltage=this.sumA*this.Vadc_3; //Voltage for the force board
    this.Force_val=this.Force_voltage*this.rangefactor;
    return this.Force_val
    }

```

}

}
