c# - Object reference not set to an instance of an object. error that I can't resolve -


this question has answer here:

i had code (this snippet):

public static cpofferinterfaceinfo get()     {         return new cpofferinterfaceinfo          {              roles = new list<role>              {                 new role                 {                     roletype = roletype.cp,                     statuses = new list<status>                     {                         new status                         {                             statusenum = statusenum.cpcreatednew,                             displayas = "sent",                             functions = { 1,2,3 }                          },                         new status                         {                             statusenum = statusenum.ncpdeclined,                             displayas = "declined",                             functions = { 4 }                          }, 

working fine earlier in day , changed 1 small thing (the function = { 1, 3, 5 } clause) , error:

object reference not set instance of object.

description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code.

exception details: system.nullreferenceexception: object reference not set instance of object.

source error:

line 11:         public static cpofferinterfaceinfo get() line 12:         { line 13:             return new cpofferinterfaceinfo line 14:              { line 15:                  roles = new list<role> 

this c# class status:

public class status     {         public statusenum statusenum { get; set; }         public string displayas { get; set; }         public icollection<int> functions { get; set; }     } 

the code compiles fails @ run time. have thoughts or experience this? can change/try/test?

i suspect had:

functions = new list<int> { 1,2, 3 } 

that sets property on status instance that's being constructed in object initializer. current code, this:

functions = { 1,2,3 } 

just calls newobject.functions.add(1); (etc) - that's not going work while functions null, default.

alternatives:

  • go explicitly creating collection
  • change status code create collection you:

    public class status {     public statusenum statusenum { get; set; }     public string displayas { get; set; }      private readonly icollection<int> functions = new list<int>;     public icollection<int> functions { { return functions; } } } 

Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -