I have a class called Foo, it has an inner class Inner. The Inner class has reference to outer class's field.
public class Foo
{
int foo = 2;
public class Inner
{
Foo f;
public Inner(Foo f)
{
f=f;
}
public override String ToString()
{
return "Inner[foo="+f.foo+"]" ;
}
}
}
This is my program.
public class Program
{
public static void Main(string[] args)
{
Foo foo=new Foo();
Foo.Inner inner=new Foo.Inner(foo);
Console.WriteLine(inner.ToString());
}
}
There is no problem to compile it, but it fails when I run it. It gives me an exception
Unhandled Exception: System.NullReferenceException: Object reference not set to an instance of an object. at Foo.Inner.ToString()
What is going on here?