2

I had this code (this is just a 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 the day and I changed one small thing (the Function = { 1, 3, 5 } clause) and now I get this error:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an 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 is the C# class for Status:

public class Status
    {
        public StatusEnum StatusEnum { get; set; }
        public string DisplayAs { get; set; }
        public ICollection<int> Functions { get; set; }
    }

The code compiles but fails at run time. Anyone have any thoughts or experience with this? What can I change/try/test?

tereško
  • 56,151
  • 24
  • 92
  • 147
vealer
  • 615
  • 5
  • 9
  • 1
    Have you tried using a debugger ? You ask it to break on every thrown exception, and you check variables : one shall be null – Kek Aug 16 '13 at 06:32

1 Answers1

7

I suspect that previously you had:

Functions = new List<int> { 1,2, 3 }

That sets the property on the Status instance that's being constructed in your object initializer. Your current code, like this:

Functions = { 1,2,3 }

just calls newObject.Functions.Add(1); (etc) - that's not going to work while Functions is null, which it is by default.

Alternatives:

  • Go back to explicitly creating a collection
  • Change your Status code to create the collection for 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 { get { return functions; } }
    }
    
Jon Skeet
  • 1,261,211
  • 792
  • 8,724
  • 8,929
  • i would say that alternative 2 is the more idiomatic one – Firo Aug 16 '13 at 08:09
  • @Firo: In some cases, certainly. In other cases it makes sense to have a writable collection property. I'm trying to avoid being too prescriptive here :) – Jon Skeet Aug 16 '13 at 09:01
  • Thanks @JonSkeet, that helped a lot. Yeah, I guess I got confused on that. I like your solution that sets the new List privately. That's cool. Thanks for your help!! – vealer Aug 16 '13 at 15:41