Posts

Showing posts from October, 2024

SPFX error Object is possibly null while using this.domElement.querySelector

Image
This post is specific to how to oversome object is possibly null issue in SPFx. I have created one simple webpart to display SharePrice. On the fly, it is going to call API and get the value and display it inside the DIV tag. As simple as that.  Below the part of the HTML in the  render() method:         <div>             <h5>SharePrice</h5>             <div name="currprice"></div>           </div>         </div>           </section>` ;     this ._getSharePrice(); Below the line of code I used to display the dynamic share price  : this .domElement.querySelector( '#currprice' )?.textContent = `Current Price: $ ${ sharePrice } ` ; This throws me below error saying object is null. Even there is single DIV elememt, still the error exist.       ...