问题
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
}
}
回答1:
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
回答2:
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.
来源:https://stackoverflow.com/questions/7932823/nullreferenceexception-was-unhandled-by-user-code