c# - Why I am getting null exception when using an Interface -


this question has answer here:

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

Popular posts from this blog

Django REST Framework perform_create: You cannot call `.save()` after accessing `serializer.data` -

Why does Go error when trying to marshal this JSON? -