|
-
Oct 23rd, 2011, 02:59 AM
#1
Thread Starter
Hyperactive Member
Null Exception - Why and how do I prevent it?
I know that a nullreferenceexception is what you get when you do something with a value that is null (kinda).
I get an exception on this row sometimes:
Code:
htmdoc.All["user"].InnerText = username;
The "username" variable is never null, so I figured it must be the htmdoc (HTMLDocument) that is the problem here. But I really don't know how to prevent it or fix it. It tries to fill that field as soon as the webpage has finished loading, when it gives a null exception I just close the program, start it again and load the same website and it works.
So what is the best way to solve this? Is it to put a try and catch around it and then loop until it doesn't give exception or?
-
Oct 23rd, 2011, 04:20 AM
#2
Re: Null Exception - Why and how do I prevent it?
Even if username was null it would not error on that line, myReference.property = null is perfectly acceptable as long as myReference is not null itself.
I would guess that the most likely problem is that htmdoc is not encapsulating the web page you think it is and there is no user tag. The internet as such should be considered to be out of your control so you should not assume anything you try to get from it is set in stone.
Last edited by Milk; Oct 23rd, 2011 at 08:40 AM.
Reason: oopsy, added missing 'property'
W o t . S i g
-
Oct 23rd, 2011, 06:14 AM
#3
Thread Starter
Hyperactive Member
Re: Null Exception - Why and how do I prevent it?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|