locked
How to resolve "SFM_GUI.js:1555 Uncaught TypeError: Cannot set property 'innerHTML' of null" RRS feed

  • Question

  • User321489315 posted

    Hello All, 

    Please help me to resolve below console error. 
    Uncaught TypeError: Cannot set property 'innerHTML' of null
    at Object.init (VM682 SFM_GUI.js:1555)
    at HTMLDocument.<anonymous> (GOGraphOperation.php?SAMSESSID=h2okb0bnu1tt8mnkun7tpou4m7&decomp=6&typeGraph=Histo&zoomGraph=Nb&order=lib&sens=ASC&num_page=&filtreTemps=6&dateDeDebut=2017-02-01&dateDeFin=2018-01-31:89)
    at Function.<anonymous> (jquery-1.3.2.min.js:19)
    at Function.each (jquery-1.3.2.min.js:12)
    at Function.ready (jquery-1.3.2.min.js:19)
    at HTMLDocument.<anonymous> (jquery-1.3.2.min.js:19)
    and below is my code snippet
    releaseOnHold : function() {
    this.elem.getElementsByTagName('div')[0].innerHTML = this.buttonTxt;
    },
    init : function(txt, width){
    debugger
    console.log(this);

    //this makes sure the object is in the GUI list
    /*if(SFM.GUI.get(this.elem) === false){
    SFM.GUI._add(this.elem, this);
    }*/
    this.elem.innerHTML ='';      //getting => Uncaught TypeError: Cannot set property 'innerHTML' of null"  error here
    this.buttonTxt = txt;
    //var txt = txt || ((this.elem.SFMDefaultTxt)?this.elem.SFMDefaultTxt : '');
    this.elem.style.width = width +"px";
    this.elem.style.height = "18px";
    this.elem.style.position = "relative";
    this.elem.style.cursor = "pointer";
    Thanks in advance
    Chandu
    Tuesday, February 13, 2018 5:28 AM

All replies

  • User-474980206 posted
    You are not check if the lookup returned any data

    var d = this.elem.getElementsByTagName('div');
    If (d.lengrh)
    d[0].innerHTML = this.buttonTxt;

    Tuesday, February 13, 2018 3:22 PM
  • User-1838255255 posted

    Hi chandu_king,

    According to your description and this exception, this error maybe caused by you want to get the value of the element, but you can not get this element,so i suggest you could debug your js code to check if get the element. About how to debug JS code, please check the following tutorial: 

    https://www.w3schools.com/js/js_debugging.asp 

    Here are someone who meet the similar issue as you, please check: 

    https://stackoverflow.com/questions/18239430/cannot-set-property-innerhtml-of-null 

    https://teamtreehouse.com/community/uncaught-typeerror-cannot-set-property-innerhtml-of-null-at-print-scriptjs29-at-scriptjs32 

    Best Regards,

    Eric Du

    Wednesday, February 14, 2018 6:44 AM
  • User521825913 posted

    In JavaScript almost everything is an object, null and undefined are exception. When you try to access an undefined variable it always returns undefined and we cannot get or set any property of undefined. In that case, an application will throw Uncaught TypeError cannot set property of undefined.

    In most cases, you are missing the initialization . So, you need to initialize the variable first. Moreover, if you are not sure a variable that will always have some value, the best practice is to check the value of variables for null or undefined before using them. If you just want to check whether there's any value, you can do:

    if( value ) {
    //do something
    }

    Or, if you do not know whether a variable exists (that means, if it was declared) you should check with the typeof operator .

    if ( typeof(some_variable) !== "undefined" && some_variable !== null ) {
    //deal with value
    }

    Monday, April 27, 2020 5:10 AM