Why does this C # assignment raise an exception?

public class A { private string _a_string; public string AString { get { return _a_string; } set { _a_string = value; } } } public class B { private string _b_string; private A _a; public AA { get { return _a; } set { _a = value; } } public string BString { get { return _b_string; } set { _b_string = value; } } } 

This does not work:

  B _b = new B { A = { AString = "aString" }, BString = "bString" }; 

System.NullReferenceException: The reference to the object is not set to the instance of the object.

It works:

  B _b = new B { A = new A { AString = "aString" }, BString = "bString" }; 

Both compile in VS2010.

+7
source share
4 answers

An instance of A is not created inside B unless you explicitly create it, as in your second example.

Change to;

 public class B { private string _b_string; private A _a = new A(); public AA { get { return _a; } set { _a = value; } } public string BString { get { return _b_string; } set { _b_string = value; } } } 

To use the first example.

In short, without the new A (), A does not exist, which is the cause of the NullReferenceException

+4
source

Line

 B _b = new B { A = { AString = "aString" }, BString = "bString" }; 

equivalently

 B _b = new B(); _b.A.AString = "aString"; // null reference here: _b.A == null _b.BString = "bString"; 

I think that in this form it is clear what is happening.

Compare this with the equivalent form of an expression that works:

 B _b = new B(); _b.A = new A(); _b.A.AString = "aString"; // just fine now _b.BString = "bString"; 
+5
source

In the first case, the code is effectively converted by the compiler to

 B b = new B(); A a = bA; a.AString = "aString"; b.BString = "bString"; 

Since you never assigned bA instance, you get an exception (in particular, since you never assigned bA , a is null, and therefore a.AString is going to throw a NullReferenceException ). The fact that code of the form new A... does not exist anywhere is a huge clue that an instance of a never created.

You can solve this problem by adding A = new A() to constructor B or by adding a field initializer to B._a .

In the second case, the code is converted by the compiler to

 B b = new B(); A a = new A(); a.AString = "aString"; bA = a; b.BString = "bString"; 

This is normal, because now we have an instance of a .

+1
source

You cannot use

 A = { AString = "aString" } 

for the same reason as you

 B _b = new B { 

you must declare an instance of B (and A) in order to use it.

If you wrote

 B = B{... 

you will get a similar error

0
source

All Articles