Hy, i'm trying to get the output text from a page, but i receive the following error:

System.Null.ReferenceException : Object reference not set to an instance of an object.

webBrowser1.Navigate(URL);
            try
            {
                string doc;
                doc = webBrowser1.Document.Body.OuterText;


                if (doc != "Invalid Arguments !")
                {
                     //DO something
                }

            }
            catch (Exception k)
            {
                MessageBox.Show(k.ToString(),"Error");
                
            }

try setting doc to String.Empty or = ""; and see if that helps. Which line specifically in that gives you the error?

try setting doc to String.Empty or = ""; and see if that helps. Which line specifically in that gives you the error?

doesn't help:

this line gives the error : doc = webBrowser1.Document.Body.OuterText;
i think the right sight gives the error.

and another thing if i try 2 times in a row, the second time works perfectly without an error.

Okay, well it definitely sounds like the right side is throwing the exception. Do me a favor, purposely throw the exception, then take two screen shots of your compiler. Once with your mouse hovered over the doc variable so I can see it's value, and the second over the words OuterText so I can see the value there. It sounds to me like OuterText is returning null or doc is already null and needs to be initialized.

Okay, well it definitely sounds like the right side is throwing the exception. Do me a favor, purposely throw the exception, then take two screen shots of your compiler. Once with your mouse hovered over the doc variable so I can see it's value, and the second over the words OuterText so I can see the value there. It sounds to me like OuterText is returning null or doc is already null and needs to be initialized.

http://hostpicture.eu/di-85R1.png
http://hostpicture.eu/di-LA93.png

So yes, it's the right side that is throwing the exception. You said that the second time it works just fine every time? That would mean it's logic related that's causing it. Probably something to do with something handling the document before you try to send it to the string. Do you have an instant messenger?

So yes, it's the right side that is throwing the exception. You said that the second time it works just fine every time? That would mean it's logic related that's causing it. Probably something to do with something handling the document before you try to send it to the string. Do you have an instant messenger?

yes the second time works, but only if it is in a try, catch block
YM: dhostro

The reason for the null reference is that it's on a button click event, the document doesn't have the required time to load before you're accessing it; therefore the document doesn't exist yet, it's just a null value. Try putting the snippet into the web browser's Document Completed event. It will allow the document to load before you access your API. Below is an example:

// Put this into the constructor for your form.
this.webBrowser1.DocumentCompleted += new System.Windows.Forms.WebBrowserDocumentCompletedEventHandler(this.webBrowser1_DocumentCompleted);

	// Put this anywhere in the source.
        private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            try
            {
                string doc;
                doc = webBrowser1.Document.Body.OuterText;


                if (doc != "Invalid Arguments !")
                {
                    //DO something
                }

            }
            catch (Exception k)
            {
                MessageBox.Show(k.ToString(), "Error");

            }
        }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.