I have to go crazy because my unit tests fail because the following code throws a null ref exception:
int pid = 0; if (parentCategory != null) { Console.WriteLine(parentCategory.Id); pid = parentCategory.Id; }
The line that throws it:
pid = parentCategory.Id;
The .writeline console is just debugging in the NUnit GUI, but it outputs a valid int.
Edit: single-threaded, so it cannot be assigned a null value from some other thread, and the fact that Console.WriteLine successfully prints this value indicates that it should not be thrown.
Edit: relevant fragments of the Category class:
public class Category { private readonly int id; public Category(Category parent, int id) { Parent = parent; parent.PerformIfNotNull(() => parent.subcategories.AddIfNew(this)); Name = string.Empty; this.id = id; } public int Id { get { return id; } } }
Well, if someone wants to see the full code, it's on Google Code at http://code.google.com/p/chefbook/source/checkout
I think I will try to restart the computer ... I saw pretty strange things fixed by rebooting. Will be updated after reboot.
Update: The mystery has been resolved. It seems that NUnit shows the error line as the last successfully executed statement ... Copy / paste the test into a new console application and run in VS, showed that it is the line after the if if block (not shown) containing null ref. Thanks to everyone for the ideas. +1 to everyone who answered.
source share