0

I was having some issues the other day with my interface lagging and after asking here someone suggested using a using statement to dispose of the webbrowser.

Now after implementing it I keep getting:

NullReferenceException was unhandled by user code- Object reference not set to an instance of an object.

I am having a weird issue because even if I am running code that has nothing to do with another I keep getting the nullreference error there which makes no sense.

Here is an example of my code:

using (System.Windows.Forms.WebBrowser webBrowser1 = 
    new System.Windows.Forms.WebBrowser())
{
    // issue happens here
    if (webBrowser1.Url.AbsoluteUri.Contains("/signup"))
    {
        // rest of the code 
    }  
}
John Saunders
  • 160,644
  • 26
  • 247
  • 397
user1017524
  • 241
  • 9
  • 17
  • Why are you using a WebBrowser in this manner? What are you trying to achieve? (There are probably much better alternatives.) WebBrowser is an object meant to display web pages in .NET Forms apps and is typically added to a Form etc. – Paul Sasik Oct 28 '11 at 17:47
  • I am trying to write data to a form. The code to write to the form is in the portion that says rest of code. – user1017524 Oct 28 '11 at 17:51
  • Have you tries just adding the browser control to the form through the designer? Things might get much simpler for you. (If you don't want to actually display it, you could hide it on the form.) – Paul Sasik Oct 28 '11 at 17:57
  • Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders May 15 '14 at 02:56

2 Answers2

6

Considering you just created the WebBrowser control and haven't set a Url yet, I'm going to go out on a limb and say that the Url is null.

Justin Niessner
  • 242,243
  • 40
  • 408
  • 536
  • How can I fix it? Like I said in the post I was having a ObjectDisposedException error and someone in another thread recommended I use a using statement. – user1017524 Oct 28 '11 at 17:52
  • 1
    @user1017524 - Not seeing the rest of your code, I have no idea what you're trying to do. It looks like you're expecting the WebBrowser control to already have a URL...but unless you set one explicitly, it's not going to. – Justin Niessner Oct 28 '11 at 17:54
  • Well the other code I have is a button that once clicked on will navigate to a site. Then with the if statement if the site has the word signup in the url it will do the code. – user1017524 Oct 28 '11 at 17:58
  • @user1017524 - You cannot reference a Url if its null, check to see if its null, and if its not then do **if (webBrowser1.Url.AbsoluteUri.Contains("/signup"))** – Security Hound Oct 28 '11 at 18:04
  • 1
    @user1017524 - It sounds like you want to use an existing WebBrowser control. In that case, instantiating one inside of a using block isn't what you want at all. – Justin Niessner Oct 28 '11 at 18:07
  • @JustinNiessner - If he was doing this on the button click that would be correct I am going to guess he isn't. – Security Hound Oct 28 '11 at 18:09
  • @Justin Then what should I do? Without it I get a disposed error? – user1017524 Oct 28 '11 at 18:10
  • @Ramhound I am navigating to a site on a button click – user1017524 Oct 28 '11 at 18:11
  • @user1017524 - Do you have a link to your original question? – Justin Niessner Oct 28 '11 at 18:20
  • @JustinNiessner http://stackoverflow.com/questions/7923893/c-objectdisposedexception-was-unhandled-by-user-code – user1017524 Oct 28 '11 at 18:24
0

You are creating a new instance of System.Windows.Forms.WebBrowser

System.Windows.Forms.WebBrowser webBrowser1 = new System.Windows.Forms.WebBrowser();

And now you are checking webBrowser1.Url....property, but actually you have don't have anything in that property. So obviously you will get nullreferenceexecption.

And if you are asking about the solution, there is no solution other than changing approach and actually I am not able to understand what you are trying to do. I mean its like purchasing a basket and start searching for apple in it assuming it has all the fruits inside it already...When you are creating a object just now, how can it have property text already in it.

it may help

Sandy
  • 11,332
  • 27
  • 76
  • 122
  • Alright now I understand. What I am doing is navigating to a site witht he click of a button. In my documentcompleted event I have a if then statement that fills out data to a form. Since yesterday my interface has been lagging and I kept getting an ObjectDisposedException and I was recommend to use a using statement. Now I understand that the using statement they gave me starts up a new browser and I am getting a null error because there is no url in the web browser. How can I make it so everything can work without getting a disposing error? – user1017524 Oct 28 '11 at 18:20
  • i am not sure why were you getting disposing error....from where are getting the URL string? cant you check the existance of "Signup" word directly in it? why to go for webbrowser.url?? – Sandy Oct 28 '11 at 18:36
  • I am getting it from Twitter. Are you saying that instead to have it for the url to have an if statement with the documenttext? – user1017524 Oct 28 '11 at 18:42
  • possibly yes....as I don't know your scenario I feel checking of string can be done before assigning it as url...by the way, why are you creating instance of webbrowser?? – Sandy Oct 28 '11 at 18:47
  • Because I was having the dispose error and someone in another thread recommended I do a using statement where I create a web browser. – user1017524 Oct 28 '11 at 18:51
  • I am not asking why are you using "using" statement – Sandy Oct 28 '11 at 18:54
  • Just tested my code using DocumentText and still the Interface lagged like crazy. It gets frozen and I have no clue why – user1017524 Oct 28 '11 at 18:54
  • The using statement creates the instance thats why metnioned the using statement. – user1017524 Oct 28 '11 at 18:55
  • it may help http://www.c-sharpcorner.com/UploadFile/kapilsoni88/WebBrowser_In_DotNet_203162009043331AM/WebBrowser_In_DotNet_2.aspx – Sandy Oct 28 '11 at 19:01
  • Nah, thanks anyways but I didnt get much help :/ – user1017524 Oct 28 '11 at 19:55
  • I solved the issue. My problem was that in my for loop I forgot to add a portion of code to it for example I had it like this: for (int i =0; i < listBox1.Items.Count;) and I forgot to put the: i++ in the end. Thanks for all the help. – user1017524 Oct 28 '11 at 21:10