For Loop

Object reference not set to an instance of an object.

Page 1 of 1

3 Replies - 508 Views - Last Post: 07 July 2009 - 01:41 PM Rate Topic: -----

#1 mager1794  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 133
  • Joined: 23-March 09

For Loop

Posted 07 July 2009 - 01:01 PM

My for loop is getting a null reference for everything involving "FileNames" for some reason can someone help

class Project
	{
		public List<string> FileNames { get; set; }
		public string ProjectName { get; set; }

		public void AddNodes(TreeNode node)
		{
			for (int i = 0; i <= FileNames.Count; i++)
			{
				node.Nodes.Add(FileNames[i]);
			}
		}

		public void LoadProject(string file)
		{
			string xmlURL = file;
			XmlTextReader reader = new XmlTextReader(xmlURL);
			try
			{

				reader.MoveToContent();
				string elementName = "";
				if ((reader.NodeType == XmlNodeType.Element) && (reader.Name == "Project"))
				{
					while (reader.Read())
					{

						if (reader.NodeType == XmlNodeType.Element)
							elementName = reader.Name;
						else
						{
							if ((reader.NodeType == XmlNodeType.Text) && (reader.HasValue))
							{
								switch (elementName)
								{
									case "Name":
										ProjectName = reader.Value;
										break;
									case "file":
										FileNames.Add(reader.Value);
										break;
									case "File":
										FileNames.Add(reader.Value);
										break;
								}
							}
						}
					}
				}
			}
			finally
			{
				reader.Close();
			}
		}



XML
<Project>
<Name>Test</Name>
<file>Random</file>
<File>Random</File>
</Project>



Is This A Good Question/Topic? 0
  • +

Replies To: For Loop

#2 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: For Loop

Posted 07 July 2009 - 01:15 PM

I added some modifications to your code so it seems to be working now. First of all the declarations:

		public List<string> FileNames = new List<string>();
		public string ProjectName = "";
		public TreeNode myNode = new TreeNode();



You have to initialize the List as a new one, so you won't get the Null Reference exception.

Then, in the for loop you have to change the verification statement from <= to < (remember, the value is incrementing by one, so if you leave it the way it is, you will get an Index Out Of Range exception).

for (int i = 0; i < FileNames.Count; i++)
			{
				node.Nodes.Add(FileNames[i]);
			}



Now it all seems to be working. The code I was using to test this is below:

			LoadProject("D:\\Temporary\\test.xml");
			AddNodes(myNode);
			treeView1.Nodes.Add(myNode);



I added a TreeView on my form called treeView1, if you are wondering where did that come from. Also, your XML file is the test.xml.
Was This Post Helpful? 0
  • +
  • -

#3 mager1794  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 3
  • View blog
  • Posts: 133
  • Joined: 23-March 09

Re: For Loop

Posted 07 July 2009 - 01:27 PM

thanks man

--Edit--

How do you add a child node? its not working for me

This post has been edited by mager1794: 07 July 2009 - 01:36 PM

Was This Post Helpful? 0
  • +
  • -

#4 Core  Icon User is offline

  • using System.Linq;
  • member icon

Reputation: 774
  • View blog
  • Posts: 5,097
  • Joined: 08-December 08

Re: For Loop

Posted 07 July 2009 - 01:41 PM

Just like this:

myNode.Nodes[i].Nodes.Add(yourNode);



Where i is the index of your root node (the one you want to add subnodes to).
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1