Wednesday, 2 October 2013

NullReferenceExeption in C# when the container is initialized

NullReferenceExeption in C# when the container is initialized

In the code listed below, I get a "NullReferenceExeption" with the error :
"Object reference not set to an instance of an object". I am completely
new to C#, but guess the error is related to the inheritance and/or the
template definitions. The list gets initialized, and when debugging I can
confirm that the list does not point to NULL. I can't figure out how to do
this in another way - Appreciate the help! (Sorry about the confusing
class names / structure) EDIT : The exeption happens here :
this.localSMT.doSomething(base.list);
public class VTEST<V>
{
public List<V> list;
public LocalSMT<V> localSMT;
public VTEST()
{
list = new List<V>();
}
}
public class VTEST_FSUB<V> : VTEST<V>
{
public VTEST_FSUB()
{
do_virtual();
}
public void do_virtual()
{
this.localSMT.doSomething(base.list);
}
}
public class VTEST_RUN : VTEST_FSUB<int>
{
public VTEST_RUN()
{
localSMT = new VTEST_SUB();
}
}
public class LocalSMT<V>
{
public LocalSMT() { }
public virtual void doSomething(List<V> value) { }
}
public class VTEST_SUB : LocalSMT<int>
{
public VTEST_SUB(){}
public override void doSomething(List<int> value) {
System.Console.WriteLine("VTEST_SUB VIRTUAL");
}
}
class Program
{
Program() {}
static void Main(string[] args)
{
VTEST_RUN run = new VTEST_RUN();
}
}

No comments:

Post a Comment