1

Basically what I am trying to do is add a customer in a customer list, in the customer list there is a property BillToContact. I would like to create an instance BillToContact for a customer.

public class Customer 
{
    public string  ID { get; set; }
    public string  AccountName { get; set; }
    public Contact BillToContact { get; set; }
}

public class BillToContact
{
    public string firstname { get; set; }
    public string LastName  { get; set; }
}

public class Contact
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

// Below is attempt to add BillToContact to Customer

public void test()
{
  List<Customer> Customer = new List<Customer>();

  Customer x = new Customer();
  x.ID   = "MyId";
  x.AccountName = "HelloAccount";
  x.BillToContact.FirstName = "Jim";
  x.BillToContact.LastName  = "Bob";

  Customer.Add(x);
}

The error for this attempt was an

Object reference not set to an instance of an object.

I have also tried to create a instance of BillToContact inside of Customer to no success. To avoid any confusion, The issue is that I am trying to make a instance if BillToContact inside the Customer list.

Alexei Levenkov
  • 98,904
  • 14
  • 127
  • 179
Jimbo Jones
  • 983
  • 4
  • 13
  • 48
  • Should have been duplicate of http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it... But have nice answer... – Alexei Levenkov Jan 26 '15 at 23:57
  • It's not the same question, the context of mine is different – Jimbo Jones Jan 27 '15 at 00:36
  • I assume you've carefully read the answer to linked question and compared "Indirect" sample to your code. It would make your question significantly better if you'd link to that question and clearly explained what steps you've taken to debug and how your case is completely different – Alexei Levenkov Jan 27 '15 at 01:24

3 Answers3

10

You have to instantiate the property member before you can set its properties:

Customer x = new Customer();
x.ID   = "MyId";
x.AccountName = "HelloAccount";
x.BillToContact = new BillToContact();
x.BillToContact.FirstName = "Jim";
x.BillToContact.LastName  = "Bob";

Just instantiating the parent class does not automatically instantiate any composed classes (unless you do so in the constructor).

BradleyDotNET
  • 60,462
  • 10
  • 96
  • 117
8

You need to instantiate the BillToContact

So either:

x.BillToContact = new BillToContact {FirstName = "Jim", LastName="Bob"};

or

 x.BillToContact = new BillToContact();
 x.BillToContact.FirstName = "Jim";
 x.BillToContact.LastName = "Bob";

Both are equivilent

Nathan Koop
  • 24,803
  • 25
  • 90
  • 125
1

Both of the other answers are correct in that the object needs to be instantiated, but I have spotted a quirk with your code. It's not necessary to create a separate class called BillToContact considering it shares exactly the same properties as the Contact class. If you need BillToContact to have more properties in addition to those already in the Contact class, you could perform inheritance by making BillToContact a sub-class of Contact.

In addition, you can even make a constructor call from within the default constructor for Customer, so that way you can immediately assign values as per my example below by knowing the object won't be null:

public class Customer 
{
    public string  ID { get; set; }
    public string  AccountName { get; set; }
    public Contact BillToContact { get; set; }

    //Contructor
    public Customer()
    {
        //Instantiate BillToContact 
        BillToContact = new Contact();
    }
}

Customer x = new Customer();
x.ID = "MyId";
x.AccountName = "HelloAccount";

//This would now work because BillToContact has been instantiated from within the constructor of the Customer class
x.BillToContact.FirstName = "Jim";
x.BillToContact.LastName  = "Bob";

Customer.Add(x);

Alternatively, you could also create a constructor for the Contact class and have it accept the first name and last names as parameters. This way you can create a new Contact object and populate both the first name and last name in one hit, see below:

public class Contact
{
    public string FirstName { get; set; }
    public string LastName { get; set; }

    public Contact() {}

    public Contact(string firstName, string lastName)
    {
        this.FirstName = firstName;
        this.LastName = lastName;
    }
}

Customer x = new Customer();
x.ID = "MyId";
x.AccountName = "HelloAccount";
x.BillToContact = new Contact("Jim", "Bob");
Customer.Add(x);
Michael
  • 451
  • 2
  • 5
  • Side note: consider joining http://codereview.stackexchange.com - suggestions to improve code are more appropriate/appreciated there... much less on SO. There is also unfortunately no "extended comment" type of post which your answer essentially is. – Alexei Levenkov Jan 27 '15 at 01:01