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?