Results 1 to 9 of 9

Thread: [RESOLVED]Object reference not set to an instance of an object.[Help]

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    297

    Resolved [RESOLVED]Object reference not set to an instance of an object.[Help]

    Ok guys. Im trying to grab a HTML element style with my program, the problem is that when the page is loaded there is no style to that element, then if I click on that element and it has a style.

    I have this

    vb.net Code:
    1. With WebBrowser1
    2.  
    3.  
    4.                 If .Document.GetElementById("idSUOther9").Style is Nothing Then
    5.                     .Document.GetElementById("i1668").InvokeMember("click")
    6.                 End If
    7.                 If .Document.GetElementById("idSUOther9").Style Is "display: none;" Then
    8.                     .Document.GetElementById("i0116")
    9.                     .Document.GetElementById("i0118")
    10.  
    11.                 End If
    12.                 tLogin.Stop()
    13.                 MessageBox.Show(.Document.GetElementById("idSUOther9").Style)
    14.                 Return

    then my program crashes here

    vb.net Code:
    1. If .Document.GetElementById("idSUOther9").Style is Nothing Then
    2.                     .Document.GetElementById("i1668").InvokeMember("click")
    3.                 End If

    since idSUOther9 there is no style it cant grab its data. How can I check is "idSUOther9" has a style without crashing my program?

    Thank you
    Last edited by simon66; Jul 20th, 2010 at 01:01 PM.

  2. #2
    Fanatic Member stlaural's Avatar
    Join Date
    Sep 2007
    Location
    Quebec, Canada
    Posts
    683

    Re: Object reference not set to an instance of an object.[Help]

    I haven't played much with the WebBrowser control and HTML elements so I don't know the best practices with these but one way to solve your issue would be to handle the exception with a try/catch block. If the exception is caught then you know there is no style for "idSUOther9".
    Alex
    .NET developer
    "No. Not even in the face of Armageddon. Never compromise." (Walter Kovacs/Rorschach)

    Things to consider before posting.
    Don't forget to rate the posts if they helped and mark thread as resolved when they are.


    .Net Regex Syntax (Scripting) | .Net Regex Language Element | .Net Regex Class | DateTime format | Framework 4.0: what's new
    My fresh new blog : writingthecode, even if I don't post much.

    System: Intel i7 920, Kingston SSDNow V100 64gig, HDD WD Caviar Black 1TB, External WD "My Book" 500GB, XFX Radeon 4890 XT 1GB, 12 GBs Tri-Channel RAM, 1x27" and 1x23" LCDs, Windows 10 x64, ]VS2015, Framework 3.5 and 4.0

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Object reference not set to an instance of an object.[Help]

    Does .Document.GetElementById("idSUOther9") return an object? If IT is producing Nothing, then the InvokeMember line will fail as well.

    Basically, an "Object Reference not set to an instance of an object" is a pretty generic error. It's basically saying you're trying to do something to an object that doesn't exist; or use a method or function of an object that doesn't exist.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    297

    Re: Object reference not set to an instance of an object.[Help]

    Quote Originally Posted by Jenner View Post
    Does .Document.GetElementById("idSUOther9") return an object? If IT is producing Nothing, then the InvokeMember line will fail as well.

    Basically, an "Object Reference not set to an instance of an object" is a pretty generic error. It's basically saying you're trying to do something to an object that doesn't exist; or use a method or function of an object that doesn't exist.
    Yes. It suppose to return a value. but it crashes since idSUOther9 has to style yet.

  5. #5
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Object reference not set to an instance of an object.[Help]

    What line crashes?

    "It suppose to return a value." -- does that mean you don't know for sure that it does? That should be the first thing you should be checking. Try debugging... when it crashes, check the values of your objects... make sure they are what you expect them to be.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  6. #6

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    297

    Re: Object reference not set to an instance of an object.[Help]

    Quote Originally Posted by techgnome View Post
    What line crashes?

    "It suppose to return a value." -- does that mean you don't know for sure that it does? That should be the first thing you should be checking. Try debugging... when it crashes, check the values of your objects... make sure they are what you expect them to be.

    -tg
    It crashes here

    vb.net Code:
    1. If .Document.GetElementById("idSUOther9").Style is Nothing Then

    because "idSUOther9" in this case doesn't have a style, so it cant grab its data.

  7. #7
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: Object reference not set to an instance of an object.[Help]

    so when you check: .Document.GetElementById("idSUOther9") .... you get "something"?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  8. #8
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: Object reference not set to an instance of an object.[Help]

    Yea, try:
    Code:
    If .Document.GetElementById("idSUOther9") IsNot Nothing AndAlso .Document.GetElementById("idSUOther9").Style is Nothing Then 
    ...
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  9. #9

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2008
    Posts
    297

    Re: Object reference not set to an instance of an object.[Help]

    Quote Originally Posted by Jenner View Post
    Yea, try:
    Code:
    If .Document.GetElementById("idSUOther9") IsNot Nothing AndAlso .Document.GetElementById("idSUOther9").Style is Nothing Then 
    ...


    It works!

    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