-
May 8th, 2013, 03:47 PM
#1
Where/How to instaniate an object to handle null reference exception C#
Hey guys kind of a simple question, I'm not exactly sure where/how to initialize a new object instance so I don't get this error. I have a class object(Contact) that has another class object(ContactInfo) and sometimes the user decides not to input(instantiate) the ContactInfo object. So later when I try to do a search via Contact.ContactInfo, I get the error. Below I have the line of code where I get the error and then I have the two classes:
foreach (var Contact in Contacts)
{
if (String.Equals(Contact._contactInfo.City.ToLower(), city, StringComparison.CurrentCulture))
{
ContactsByCity.Add(Contact);
}
}
// and then the two classes:
public class Contact : Person
{
private ContactInfo info;
private ContactInfoLoader loader;
public ContactInfo _contactInfo { get; set; }
public Contact()
{ }
public Contact(ContactInfo _info)
{
info = _info;
}
public ContactInfo GetContactInfo()
{
loader = new ContactInfoLoader(this);
return loader.GatherContactInfo();
}
}
public class ContactInfo
{
public string PhoneNumber { get; set; }
public string Address { get; set; }
public string City { get; set; }
public string State { get; set;}
public ContactInfo()
{ }
}
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|