Results 1 to 3 of 3

Thread: Null Exception - Why and how do I prevent it?

  1. #1

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    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?

  2. #2
    Cumbrian Milk's Avatar
    Join Date
    Jan 2007
    Location
    0xDEADBEEF
    Posts
    2,448

    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

  3. #3

    Thread Starter
    Hyperactive Member Cyb3rH4Xter's Avatar
    Join Date
    May 2009
    Location
    Sweden
    Posts
    449

    Re: Null Exception - Why and how do I prevent it?

    Okay, thank you.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width