Null reference exception
-
Hi, when i run this code i get a null-reference exception at the last line of code. I don't know why this happens, because i first add the node and it IS there. I looked in the debugger. Does someone know a solution?
foreach (DateTime d in daten) { if (treeView1.Nodes.Find(d.Month.ToString(), false) != null) { treeView1.Nodes.Add(new TreeNode(d.Month.ToString())); } treeView1.Nodes[d.Month.ToString()].Nodes.Add(new TreeNode(d.Date.ToShortDateString())); }
-
Hi, when i run this code i get a null-reference exception at the last line of code. I don't know why this happens, because i first add the node and it IS there. I looked in the debugger. Does someone know a solution?
foreach (DateTime d in daten) { if (treeView1.Nodes.Find(d.Month.ToString(), false) != null) { treeView1.Nodes.Add(new TreeNode(d.Month.ToString())); } treeView1.Nodes[d.Month.ToString()].Nodes.Add(new TreeNode(d.Date.ToShortDateString())); }
It will be easier to see what's going on if you break out that last line of code as follows: TreeNode node = treeView1.Nodes[d.Month.ToString()]; TreeNode newNode = new TreeNode(d.Date.ToShortDateString()); node.Nodes.Add(newNode); This will allow you to check state as you step though in debug mode. When you put so much into a single line, it can be much less obvious what is breaking.
-
Hi, when i run this code i get a null-reference exception at the last line of code. I don't know why this happens, because i first add the node and it IS there. I looked in the debugger. Does someone know a solution?
foreach (DateTime d in daten) { if (treeView1.Nodes.Find(d.Month.ToString(), false) != null) { treeView1.Nodes.Add(new TreeNode(d.Month.ToString())); } treeView1.Nodes[d.Month.ToString()].Nodes.Add(new TreeNode(d.Date.ToShortDateString())); }
if d is null from daten then d.Month will fail; Also, treeView1.Notes[someString] may return null as well but it is most likely the d;
File Not Found
-
if d is null from daten then d.Month will fail; Also, treeView1.Notes[someString] may return null as well but it is most likely the d;
File Not Found
-
Ennis Ray Lynch, Jr. wrote:
but it is most likely the d
Not so very likely... The variable d is a DateTime structure, and can't be null.
--- single minded; short sighted; long gone;
Since daten is not defined in could very well be a non-generic 1.1 .NET collection such as an array list which are collections of objects. Iterating on such a structure would attempt to assign null giving the null reference exception. Below is written in 2.0 but uses the 1.1 collection ArrayList.
ArrayList list = new ArrayList(); list.Add(DateTime.Now); list.Add(null); foreach(DateTime d in list) { MessageBox.Show(d.ToShortDateString()); }
File Not Found
-
Since daten is not defined in could very well be a non-generic 1.1 .NET collection such as an array list which are collections of objects. Iterating on such a structure would attempt to assign null giving the null reference exception. Below is written in 2.0 but uses the 1.1 collection ArrayList.
ArrayList list = new ArrayList(); list.Add(DateTime.Now); list.Add(null); foreach(DateTime d in list) { MessageBox.Show(d.ToShortDateString()); }
File Not Found
-
No, it couldn't. If the daten collection would contain something other than DateTime values, the error would appear when the value is put in the d variable, not when the d variable is later used.
--- single minded; short sighted; long gone;
Perhaps but when a question is posed so vague as to say where is my null reference exception i think any little bit of help will help. Now you are correct in that when I said d.Month I was wrong. Sometimes I say the stupidest things. Of course, that could also fall into the category of someone designing an OO language that doesn't fully support nulls until the third release.
File Not Found