Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Object Reference not set to an instant of the object 3

Status
Not open for further replies.

seabaz2000

IS-IT--Management
Feb 24, 2006
79
IE
Hi
I keep getting the error "Object Reference not set to an instant of the object" with the following piece of code
Code:
XmlNode model = modelDoc.SelectSingleNode(strXPath);

strName = (string)model.Attributes["name"].Value;

not quite sure what is wrong with it.
Regards
 
Here is modelDoc
Code:
XmlDocument modelDoc = new XmlDocument();
            modelDoc.LoadXml(modelList);

            string strXPath = "//Model[@modelID='" + strModelId + "']";
            XmlNode model = modelDoc.SelectSingleNode(strXPath);
The problem piece of code seems to be
Code:
strName = (string)model.Attributes["name"].Value;
Thanks for the help
 
I think it should be String not string and is it model that is null or is it model.attibutes["name"] that is null.

Christiaan Baes
Belgium

"My new site" - Me
 
try

Code:
strName = model.Attributes["name"].Value.ToString();

the cast could apply to the model, and a string doesn't have an Attribute.



mr s. <;)

 
Either model is null or the attribute "name" does not exist returning a null, as chrissie1 pointed out.

PS. Attribute[x].Value returns string so no need to cast.

[wink]
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top