Link to home
Start Free TrialLog in
Avatar of MellowD0c
MellowD0c

asked on

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?
ASKER CERTIFIED SOLUTION
Avatar of jaynus
jaynus

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of BurntSky
BurntSky

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.
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.
Good Habit you should good into period :)
Avatar of MellowD0c

ASKER

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.