tags:

views:

58

answers:

2

Ok so this is some code im writing to help me out on a game, anyway this stumped me...

ok so this calls my method:

    private void webBrowser1_Navigated(object sender, WebBrowserNavigatedEventArgs e)
    {
        if (webBrowser1.Document.Url.OriginalString.Contains(@"page=logs"))
        {
            AttatchProcess Test = new AttatchProcess();
            Test.LogCleanser(webBrowser1);
        }
    }

And heres the method:

    public void LogCleanser(WebBrowser TargetBrowser)
    {

        if (TargetBrowser.Url.OriginalString.Contains(@"page=logs"))
        {


            Regex FindIP = new Regex(IPExpression);
            HtmlElement LogArea = TargetBrowser.Document.GetElementsByTagName("textarea")[0];
            string log = LogArea.InnerHtml.ToString();

            foreach (Match ipfound in FindIP.Matches(log))
            {
                if (ipfound.Value == MyIP)
                {
                    log.Replace(ipfound.Value, "");
                }
            }

        }

So as soon as the called method hits a line of code assigning an objects value to something in the documents HTML it ends the method, in this case the line:

HtmlElement LogArea = TargetBrowser.Document.GetElementsByTagName("textarea")[0];

is hit and kills the method, any help and ideas much appriciated!

A: 

I would guess that there are no <textarea/> elements in the document, therefore causing

TargetBrowser.Document.GetElementsByTagName("textarea")

to be null and your attempt to index the elements array (...ByTagName("textarea")[0]) is causing a NullReferenceException to be throw.

This is all conjecture at this point as your question does not provide enough evidence to know for certain. Are any exceptions being thrown? If so, of what type?

Andrew Hare
No exceptions are being thrown atall which is why im so confused! the code just stops and drops out of the method, any other information you may find usefull let me know
Yoda
A: 

Is log an empty string?

What does FindIP.Matches(log) return?

If log is empty or your regex returns no matches, that would explain why it "drops out".

Eric J.
The code isnt even making it down to the point of assigning "log" a value, its dropping out on the..HtmlElement LogArea = TargetBrowser.Document.GetElementsByTagName("textarea")[0];line, the page deffinately has a textarea tag as ive used the same code before with no problems on the same webpage
Yoda
How do you know? Single-stepping in debugger? If so, inspectTargetBrowser.Document.GetElementsByTagName("textarea")
Eric J.