Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

C#

Patrick McLeod
Patrick McLeod
5,913 Points

Trouble with C# Address Book Challenge System.NullReferenceException Null reference

Hi I´ve an issue on line 84 of AddressBook.cs. The error I get is a System.NullReferenceException that says that contact is null.

I have a Method called SearchEntries that runs a foreach loop meant to look up a contact´s name in an array and return the contact´s details. I am unable to have the contact name which I believe I input in Program.cs, lines 55 & 57 pass into the method and this is the reason for the null error.

I´d really appreciate some help on this as I´ve been stuck for a while. See the code here: https://w.trhou.se/trhmijyt0n As you might have picked up, I´m working with Visual Studio as mi IDE...

1 Answer

Steven Parker
Steven Parker
231,109 Points

With Visual Studio, you have a lot of debugging resources not available in the workspace. You might try setting breakpoints before the call to SearchEntries and/or at the end of NewEntry and while paused you can look at the contents of "contacts", particularly the "people" property..

At first glance I had a few questions:

  • why would a "foreach" return a null instead of just not iterating?
  • what's in the "people" array when "contacts" is first created? (breakpoint there and check!)
  • does it have a non-zero length even when empty?
  • where is new data added to the people array (I couldn't spot it)?
Patrick McLeod
Patrick McLeod
5,913 Points

Hi Steven, thanks for getting back to me so quickly, my answers are below:

  1. In essence , that is the same question I have, why am I getting "null" back?

  2. I was thinking about that last night and perhaps I´m making this more complicated that it should.

  3. When it is empty it does have a 0 length, at the beginning if I enter "count" as an option in the if-else loop (Program.cs, line 71) and it confirms that I have 0 Contacts. After I create the two contacts I always test with,and enter "count" again, the loop indicates I have O space available for new contacts.

  4. The new data is added after I type "new" in the options in the if-else loop (Program.cs, line 39). That runs the NewEnty method (Program.cs, line 43). the code for NewEntry() is in AddressBook.cs, line 37.

Thank you for you reply and help, I'm sorry if the code is a mess or if I haven't answered your questions as thoroughly you would have wished.