1

I am trying to nest a list of objects in a list; I have tried the following:

Instantiation:

  public static List<PhonePeople> Helpdesk, Admins = new List<PhonePeople>();
  public static List<List<PhonePeople>> PDepartments = 
          new List<List<PhonePeople>>{Helpdesk,Admins};

Attempting to add to the helpdesk list as follows:

 MainWindow.PDepartments[counter].Add(thisPerson);

Error:

"Object reference not set to an instance of an object"

I defined counter to 0 , 0 is the index for helpdesk. Mainwindow is where the static list resides. I believe there is an issue with my List of list's or the way i am adding to it;

whats wrong?

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
ModS
  • 806
  • 2
  • 15
  • 28
  • 2
    Note that I think it is not direct duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) as it more about declaring multiple variables in single statement. @Mods, consider reading the linked question to help you with debugging this type f exception (now and later). – Alexei Levenkov Sep 26 '14 at 15:16
  • Thanks Alexei; Will do this next time, I skimmed the titles but was close minded on "nested lists" instead of all the information involved. – ModS Sep 26 '14 at 16:20

1 Answers1

6

Your line public static List<PhonePeople> Helpdesk, Admins = new List<PhonePeople>(); is only instantiating Admins not HelpDesk

Jeffrey Wieder
  • 2,336
  • 1
  • 14
  • 12
  • 1
    Exactly what I was just about to write :-) – Pheonyx Sep 26 '14 at 15:01
  • Oh! how do you chain these? helpdesk = admins = new list ? Thanks by the way this has been a problem for a while! – ModS Sep 26 '14 at 15:03
  • 2
    No, that would point the variables to the same object, you need a separate instance for each object reference, so you need two `new` statements. – Charleh Sep 26 '14 at 15:04