locked
NullReferenceException? Not sure where its occuring... RRS feed

  • Question

  • Everytime i load a new page in my browser to block the ads, i get a systemnullreference exception. It gets so far into the code and gives me the exception. I will place a *** next to where i get my exception. I tried to debug but thats all i can get out of it. All help would be appreciated.

    public void BlockAds()
            {
                HTMLDocument doc = (HTMLDocument)IbrowserManager.CurrentBrowser.Document.DomDocument;
                string s;
    
                foreach (IHTMLImgElement imge in doc.images)
                {
                    try
                    {
                        s = imge.src;
    
                        foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                        {
                            if (s.Contains(adurls))
                            {
                                Remove(imge as IHTMLDOMNode);
                            }
                        }
    
                    }
                    catch (Exception)
                    {
                        //do nothing
                    } 
                }
    
                foreach (IHTMLEmbedElement embedse in doc.embeds)
                {
                    try 
                	{
                        s = embedse.src;
    
                        foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                        {
                            if (s.Contains(adurls))
                            {
                                Remove(embedse as IHTMLDOMNode);
                            }
                        }
    
                	}
                	catch (Exception)
                	{
                        //do nothing
                	}   
                }
    
                foreach (IHTMLScriptElement scripte in doc.scripts)
                {
                    try 
                	{
                        s = scripte.src;
    
                        foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                        {
                            if (s.Contains(adurls))****
                            {
                                Remove(scripte as IHTMLDOMNode);
                            }
                        }
    
                	}
                	catch (Exception)
                	{
                        //do nothing
                	}   
                }
    
                foreach (IHTMLElement linkse in doc.links)
                {
                    try
                    {
                        if (linkse is IHTMLLinkElement)
                        {
                            s = (linkse as IHTMLLinkElement).href;
    
                            foreach (string adurl in Properties.Settings.Default.AdBlockURLS)
                            {
                                if (s.Contains(adurl))
                                {
                                    Remove(linkse as IHTMLDOMNode);
                                }
                            }
                        }
                        else if (linkse is IHTMLAnchorElement)
                        {
                            s = (linkse as IHTMLAnchorElement).href;
                            foreach (string adurl in Properties.Settings.Default.AdBlockURLS)
                            {
                                if (s.Contains(adurl))
                                {
                                    Remove(linkse as IHTMLDOMNode);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //do nothing
                    }
                }
    
                foreach (IHTMLElement4 dive in doc.getElementsByTagName("DIV"))
                {
                    try
                    {
                        s = dive.getAttributeNode("class").nodeValue as string;
    
                        foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                        {
                            if (s.Contains(adurls))
                            {
                                Remove(dive as IHTMLDOMNode);
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //do nothing
                    }
                }
    
                foreach (IHTMLElement2 e in doc.getElementsByTagName("OBJECT"))
                {
                    foreach (IHTMLElement4 c in e.getElementsByTagName("PARAM"))
                    {
                        try
                        {
                            if ("Src".Equals((string)c.getAttributeNode("NAME").nodeValue, StringComparison.CurrentCultureIgnoreCase) || "Movie".Equals((string)c.getAttributeNode("NAME").nodeValue, StringComparison.CurrentCultureIgnoreCase))
                            {
                                foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                                {
                                    s = c.getAttributeNode("VALUE").nodeValue as string;
                                    if (s.Contains(adurls))
                                    {
                                        Remove(c as IHTMLDOMNode);
                                    }
                                }
                            }
                        }
                        catch (Exception)
                        {
                            //do nothing
                        }
                    }
                }
    
                foreach (IHTMLElement4 iframee in doc.getElementsByTagName("IFRAME"))
                {
                    try
                    {
                        s = iframee.getAttributeNode("src").nodeValue as string;
                        foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                        {
                            if (s.Contains(adurls))
                            {
                                Remove(iframee as IHTMLDOMNode);
                            }
                        }
    
                    }
                    catch (Exception)
                    {
                        //do nothing
                    }
                }
    
                foreach (IHTMLElement4 framee in doc.getElementsByTagName("FRAME"))
                {
                    try
                    {
                        s = framee.getAttributeNode("src").nodeValue as string;
                        foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                        {
                            if (s.Contains(adurls))
                            {
                                Remove(framee as IHTMLDOMNode);
                            }
                        }
    
                    }
                    catch (Exception)
                    {
                        //do nothing
                    }
                }
            }
    
            private void Remove(IHTMLDOMNode n)
            {
                try
                {
                    n.parentNode.removeChild(n);
                }
                catch (Exception)
                {
                    //do nothing
                }
            }
        }

    OMG, its Joe Ginley!
    Friday, November 27, 2009 7:41 PM

Answers

  • if scripte cannot be cast to IHTMLDOMNode then

                      scripte as IHTMLDOMNode

    will return null

    if it does then it is likely that:

                       Remove(scripte as IHTMLDOMNode);

    is throwing your NullReferenceException


    • Proposed as answer by jgalley Wednesday, December 2, 2009 4:08 PM
    • Marked as answer by JoeGinley Wednesday, December 2, 2009 8:20 PM
    Monday, November 30, 2009 3:37 PM

All replies

  • what are the value of "s" and "adurls" when the exception is thrown?

                            if (s.Contains(adurls))
                            {
                                Remove(scripte as IHTMLDOMNode);
                            }
    

    Friday, November 27, 2009 10:23 PM
  • s is a string the value of the tagname src or href.

    adurls is a stringcollection which has patterns in it so if s contains any of the patterns it will remove the node otherwise continue to the next node.
    OMG, its Joe Ginley!
    Friday, November 27, 2009 10:41 PM
  • Yes, but what are the actual values.
    If s happens to be null then s.Contains() is going to have a problem.
    • Marked as answer by JoeGinley Saturday, November 28, 2009 5:09 AM
    • Unmarked as answer by JoeGinley Saturday, November 28, 2009 6:55 AM
    Saturday, November 28, 2009 3:59 AM
  • ahh your right because not every element has a src attribute, thanks that helps.

    OMG, its Joe Ginley!
    Saturday, November 28, 2009 5:09 AM
  • How can i get around this? just use an if statement and check if s is null or != ""
    OMG, its Joe Ginley!
    Saturday, November 28, 2009 5:59 AM
  • I have fixed up the code and it shouldnt give me the exception but still does?

    foreach (IHTMLImgElement imge in doc.images)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(imge.src))
                        {
                            source = imge.src;
    
                            foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                            {
                                if (source.ToLower().Contains(adurls))
                                {
                                    Remove(imge as IHTMLDOMNode);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //do nothing
                    }
    
                    Application.DoEvents();
                }

    OMG, its Joe Ginley!
    Saturday, November 28, 2009 6:54 AM
  • Ok i found out my problem, the nullexception occurs in this part of the code, but i have tried using the same method as above but still get a nullexception, where would it be coming from or why?

    foreach (IHTMLScriptElement scripte in doc.scripts)
                {
                    try
                    {
                        if (!string.IsNullOrEmpty(scripte.src))
                        {
                            source = scripte.src;
    
                            foreach (string adurls in Properties.Settings.Default.AdBlockURLS)
                            {
                                if (source.ToLower().Contains(adurls) == false)
                                {
                                    Remove(scripte as IHTMLDOMNode);
                                }
                            }
                        }
                    }
                    catch (Exception)
                    {
                        //do nothing
                    }
    
                    Application.DoEvents();
                }
    • Edited by JoeGinley Saturday, November 28, 2009 7:19 AM change code
    Saturday, November 28, 2009 7:08 AM
  • check scripte.

    scripte as IHTMLDOMNode

    can be a problem if the cast is not valid.
    Monday, November 30, 2009 4:20 AM
  • I dont get any cast errors just null reference. Also why would that work for the images but not the scripts? Ill try to mess with that but i dont think thats it.
    OMG, its Joe Ginley!
    Monday, November 30, 2009 4:22 AM
  • if scripte cannot be cast to IHTMLDOMNode then

                      scripte as IHTMLDOMNode

    will return null

    if it does then it is likely that:

                       Remove(scripte as IHTMLDOMNode);

    is throwing your NullReferenceException


    • Proposed as answer by jgalley Wednesday, December 2, 2009 4:08 PM
    • Marked as answer by JoeGinley Wednesday, December 2, 2009 8:20 PM
    Monday, November 30, 2009 3:37 PM