OK, so I have created a dll project. This dll project uses a tree class structure to hold data, and has functions that are used against that data.
so in the dll project we have:
namespace Engine
{
public class Engineclass
{
public Component SolutionTree = new Component();
public Component ReturnComponent(string ComponentName)
{
Component CurrentComp = SolutionTree;
if(CurrentComp.Name != ComponentName)
{
CurrentComp = RecurseForName(CurrentComp, ComponentName);
}
return CurrentComp;
}
public Component RecurseForName(Component CompORG, ComponentName)
{
Component FoundComp = null;
if (!string.IsNullOrEmpty(ComponentName) && (CompORG != null))
{
if (CompORG.ComponentName.ToUpper().Trim() ==
ComponentName.ToUpper().Trim())
{
return CompORG;
}
foreach (var CompNew in CompORG.ActiveComponents)
{
var FoundComp2 = RecurseForName(CompNew, ComponentName);
if (FoundComp2 != null)
{
return FoundComp2;
}
}
}
return FoundComp;
}
}
}
The Component class is:
public class Component
{
public string Name {get; set;}
public List<Component> ActiveComponents { get; set; }
public Component()
{
this.ActiveComponents = new List<ActiveComponent>();
}
}
Now in my Console app I am testing it on, I load the dll and populate the solution tree. i.e.
EngineClass EC = new EngineClass();
EC.Component NewComp = new EC.Component();
EC.Name = "bob";
EC.Component NewSubComp = new EC.Component();
EC.Name = "Son of Bob";
NewComp.ActiveComponents.add(NewSubComp);
EC.SolutionTree = NewComp;
All works fine.
BUT, when I try to use the recursive function to return a specific component I get a System.NullReferenceException: 'Object reference not set to an instance of an object.'
var FoundASon = EC.FindComponent("Son of Bob");
and it occurs on the return CompORG;
line in the RecurseForName
function.
So, I know CompOrg
is not null, and the name in CompOrg
has matched, but trying to return CompOrg
causes the nullreferenceexception!
I really need to help on this issue, as it is so hard to debug a compiled dll.
p.s. the dll project has unit tests and they pass the functions as working just fine.