Reputation: 537
Can somebody tell me why this is throwing a null reference exception (object ref not set to an instance of an object). My code reads a simple xml file then should pass the text within 2 elements to another method. However the exception is thrown at the first line of the foreach loop:
Please excuse my ignorance, i'm new. :)
private void openProjectToolStripMenuItem_Click(object sender, EventArgs e)
{
//I have to pass 'this' (which is Form1) when creating the Projects (Form2) in order for them to understand / see each other
Projects myProjects = new Projects(this);
//displays the Form 2 (called Projects)
myProjects.Show();
XmlDocument xdoc = new XmlDocument();
xdoc.Load("O:\\TestDaws\\projects.xml");
string projList = "/Projects/Project";
XmlNodeList xprojects = xdoc.SelectNodes(projList);
foreach (XmlNode xNodeName in xprojects)
{
string projectname = xNodeName.SelectNodes("/ProjectName")[0].InnerText.ToString();
string projecttype = xNodeName.SelectNodes("/ProjectType")[0].InnerText.ToString();
myProjects.buildProjectList(projectname, projecttype);
}
}
Upvotes: 1
Views: 833
Reputation: 39284
Change the SelectNodes("/ProjectName")
into SelectNodes("./ProjectName")
. (Ditto for /ProjectType
of course)
/ProjectName
will always select from the root, not from the node you are calling it on. The ./
starts from the curent node.
Upvotes: 2
Reputation: 647
This means that xprojects
is null.
Could you, please, publish your projects.xml file content.
Regards,
Omar
Upvotes: 0