c# - Why I am getting null exception when using an Interface -
this question has answer here:
- what nullreferenceexception, , how fix it? 33 answers
public class blah { public bool whatever { get; set; } public string whatyoujustsaid { get; set; } } public interface iblah { blah blahvalues { get; set; } } class class1:iblah { public blah blahvalues { get; set; } }
and example:
class1 c1 = new class1(); c1.blahvalues.whatyoujustsaid = "nothing"; c1.blahvalues.whatever = false;
so how should change code blahvalues
doesn't null?
you have initialize blahvalues
. using object initializer, can done below:
class1 c1 = new class1() { blahvalues = new blah() };
as create c1
object, blahvalues
it's default value, null
. hence, when try assign value whatyoujustsaid
, whatever
null reference exception.
Comments
Post a Comment