• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 911
  • Last Modified:

Object reference not set to an instance of an object.

public void SetValue(string elementID, string value)
            {
                  
                        mshtml.HTMLDocument myobj = (mshtml.HTMLDocument) axWebBrowser1.Document;
                        //mshtml.HTMLInputElement SD = new mshtml.HTMLInputElement();
                         mshtml.HTMLInputElement SD = (mshtml.HTMLInputElement) myobj.getElementById(elementID.ToString());
                        SD.value = value;
                  }

Following error is generated by using above code:

Object reference not set to an instance of an object.
myTCM
   at myTCM.frmChild1.SetValue(String elementID, String value) in c:\solutions\mytcm\forms\frmchild1.cs:line 383


Exact same thing happens in yet another simiar method:

public void SelectValue(string elementID, string value)
            {
                  
                        mshtml.HTMLDocument myobj = (mshtml.HTMLDocument) axWebBrowser1.Document;
                        //mshtml.HTMLSelectElement SD = new mshtml.HTMLSelectElement();
                        mshtml.HTMLSelectElement SD = (mshtml.HTMLSelectElement) myobj.getElementById(elementID.ToString());
                        SD.value = value;
                  }

Anyone got any idea what i am doing wrong here?
0
MellowD0c
Asked:
MellowD0c
  • 2
  • 2
1 Solution
 
jaynusCommented:
The main problem I see here, is you arnt checking to make sure the elements are getting pulled. you have a getElementById function call, but you arnt checking to make sure the element is being pulled correctly.  I have found Id's to be quirky in general, or this might be executing before the element exists. Whatever it is...

elementID doesnt exist in the document its pulling, and thats why its dying on SD.value, its null.

you would want to add a check of if(SD != null) /* do operations */

This is failing for either the Document itself, or the element your trying to pull. Its whichever code is line 383, the document isnt loaded, or the element doesnt exist.

I dont know what else is going on, but I can tell you positively that is your issue

Hope that helps!!

-jaynus
0
 
BurntSkyCommented:
Which line is #383?  Either the axWebBrowser1.Document object is null (and therefore myobj becomes null) or SD is null because the element "elementID" refers to doesnt exist.  Step through it with the debugger and look for a null reference.
0
 
BurntSkyCommented:
ohh.. you beat me to it by one minute!  But yeah, I agree with jaynus.  Be sure to check for nulls before you try to manipulate objects.
0
 
jaynusCommented:
Good Habit you should good into period :)
0
 
MellowD0cAuthor Commented:
Thanks Jaynus, once again.

Had that and webbrowser navigate code all in IntitializeComponent. Perhaps it was trying to set those values even before the page got loaded, so jaynus' following did help:

This is failing for either the Document itself, or the element your trying to pull. Its whichever code is line 383, the document isnt loaded, or the element doesnt exist.


Thanks to BurntSky for the help as well.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

Cloud Class® Course: Certified Penetration Testing

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now