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(eleme ntID.ToStr ing());
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(S tring elementID, String value) in c:\solutions\mytcm\forms\f rmchild1.c s: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(eleme ntID.ToStr ing());
SD.value = value;
}
Anyone got any idea what i am doing wrong here?
{
mshtml.HTMLDocument myobj = (mshtml.HTMLDocument) axWebBrowser1.Document;
//mshtml.HTMLInputElement SD = new mshtml.HTMLInputElement();
mshtml.HTMLInputElement SD = (mshtml.HTMLInputElement) myobj.getElementById(eleme
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(S
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
mshtml.HTMLSelectElement SD = (mshtml.HTMLSelectElement)
SD.value = value;
}
Anyone got any idea what i am doing wrong here?
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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 :)
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.
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.