Cannot read property removeChild of null in jsni

2.4k views Asked by At

I am working on gwt. I write a piece of code to show a popup, this popup occurs perfectly fine and it goes automatically after 5 sec and if someone click of the popup then it will stay till user clicked outside the popup(clicked on the html body).

Whole functionality working properly but it gives an error after it gone

Error is

Uncaught TypeError: Cannot read property 'removeChild' of null

public static void popupError(){

    var sr,sm,mb1;

    sr=$doc.getElementById('errorpopup');
    sr.innerHTML="";

    sm=$doc.createElement("div");
    sm.className="greyout";
    sm.setAttribute("style","margin-bottom: 10px;");

    mb1=$doc.createElement("div");
    mb1.className="mob-maskbody";

        var rightside = $doc.createElement("div");
        rightside.className="pad10";

            var errortext= $doc.createElement("div");
            errortext.className="nh6 center bold";
            errortext.innerHTML=error;

            rightside.appendChild(errortext);

    mb1.appendChild(rightside);

    sm.appendChild(mb1);
    sr.appendChild(sm);

    fadding();

    $doc.body.onclick = function()
    {
        fadding();
    }

    function fadding()
    {
        if(sr.childNodes[0]!=null || sm!=null)
        {
            var hideFadeTimer;
            var hideDelayTimer;

            hideFadeTimer = setTimeout(function()
            {
                if(sm!=null){
                    sm.className="greydout";
                }
            },2500);

            hideDelayTimer = setTimeout(function()
            {
                if(sm!=null){
                    sm.parentNode.removeChild(sm);

                    hideFadeTimer = null;
                    hideDelayTimer = null;
                    $doc.body.onclick = null;
                }
            },5000);

            sm.onclick = function(event)
            {
                event.stopPropagation();

                if(hideDelayTimer)
                {
                    clearTimeout(hideDelayTimer);
                }
                if(hideFadeTimer)
                {
                    clearTimeout(hideFadeTimer);
                }
                sm.className="greyout";
            }
        }
    }
}
1

There are 1 answers

0
Thomas Broyer On

In case, say, you click outside the popup twice, then the hideDelayTimer will be called twice, and the second time sm will have no parent node.

You check every time whether sm is null, but you never set it to null; that might be the problem here: set it to null after you removed it from its parent node (I believe you can do much better about your event handling though).