3
Considering two classes in C#:
public class aClass
{
public string afield;
}
public class bClass
{
public aClass[] bfield;
}
I plan to start the variable as
bClass cVar = new bClass();
cVar.bfield[0].afield = "text";
but is generating the following error during debug.
System.Nullreferenceexception: 'Object Reference not set to an instance of an Object.'
cVar.bfield was null.
How I start the variable to avoid this error?
Our language is the Português, translate your question.
– user28595
Translate your question so I can signal you as a duplicate of
O que é NullReferenceException
. Responding: You need to initialize the fieldbfield
or in the field declaration itself or in the constructor. Thus:bfield = new [] { new aClass() { afield = "conteúdo" } }
– Diego Rafael Souza