?
Solved

Object reference not set to an instance of an object.

Posted on 2005-04-28
5
Medium Priority
?
894 Views
Last Modified: 2012-06-27
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
Comment
Question by:MellowD0c
  • 2
  • 2
5 Comments
 
LVL 4

Accepted Solution

by:
jaynus earned 200 total points
ID: 13889613
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
 
LVL 9

Expert Comment

by:BurntSky
ID: 13889625
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
 
LVL 9

Expert Comment

by:BurntSky
ID: 13889638
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
 
LVL 4

Expert Comment

by:jaynus
ID: 13889662
Good Habit you should good into period :)
0
 

Author Comment

by:MellowD0c
ID: 13889951
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

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We all know that functional code is the leg that any good program stands on when it comes right down to it, however, if your program lacks a good user interface your product may not have the appeal needed to keep your customers happy. This issue can…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
When cloud platforms entered the scene, users and companies jumped on board to take advantage of the many benefits, like the ability to work and connect with company information from various locations. What many didn't foresee was the increased risk…
With just a little bit of  SQL and VBA, many doors open to cool things like synchronize a list box to display data relevant to other information on a form.  If you have never written code or looked at an SQL statement before, no problem! ...  give i…
Suggested Courses

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question