I'm a beginner and I'm coding an easy Windows Form program in c# (well not that easy for me, but for an expert programmer) that works like this: a form (ContactForm class) gets input (first name, last name, address, email and phone n) from the user and stores it into variables in the Contact class through the Properties. The Contact class calls properties and methods from other three classes (Address, Email and Phone) to store and validate the user's input. The Contact class contains also an override ToString method that formats the variables containing the user's input into a string. At this point, I tried calling this ToString method with the ContactForm and displaying the string in a MessageBox and it worked perfectly. But then I tried calling the method with another class, Customer, that has an object of the Contact class, and it was a disaster. The compiler keeps relevating a NullReferenceException for the object of the Contact class (m_contact) that says is null. Here is the code:
public class Customer
{
private Contact m_contact;
private int m_ID;
public Customer()
{
}public Customer(Contact contactIn)
{
}public Customer(Contact contactIn, int id)
{
if (m_contact != null)
{
m_contact = new Contact();
m_contact = contactIn;
}
m_ID = id;
}public Contact ContactData
{
get { return m_contact; }
set {
if (value != null)
m_contact = value; }
}public int ID
{
get { return m_ID; }
set { m_ID = value; }
}
public override string ToString()
{
string strOut = string.Empty;
**if ((!string.IsNullOrEmpty(m_contact.ToString())) && (m_contact != null) && (m_contact.Validate()))**
{
strOut = m_contact.ToString();
}
else
strOut = null;
return strOut;
}
}
} The line in asterisks is the one where the error is found. And this one is the m_contact.Validate method:
public bool Validate()
{
bool ok = false;
if (!string.IsNullOrEmpty(m_firstName) && (!string.IsNullOrEmpty(m_lastName)) && m_address.CheckData() && m_email.CheckEmail() && m_phone.CheckPhone())
ok = true;
else
ok = false;
return ok;
}
Just for better clarity, this is the email.CheckEmail method, but those checking methods above are definitely not the problem:
public bool CheckEmail()
{
bool ok = false;
if ((!string.IsNullOrEmpty(m_personal)) || (!string.IsNullOrEmpty(m_work)))
ok = true;
else
ok = false;
return ok;
}
And this is the code from the ContactForm class that displays the ToString method of the Contact class with absolutely no problem:
public partial class ContactForm : Form
{
private Contact m_contact;
private bool m_closeForm;
public ContactForm(string title)
{
InitializeComponent();
m_contact = new Contact();
this.Text = title;
InitializeGUI();
}public Contact ContactData
{
get { return m_contact; }
set
{
if (value != null)
m_contact = value;
}
}private void InitializeGUI()
{
txtFirstName.Text = string.Empty;
txtLastName.Text = string.Empty;
txtHomePhone.Text = string.Empty;
txtCellPhone.Text = string.Empty;
txtEMailBus.Text = string.Empty;
txtEMailPr.Text = string.Empty;
txtStreet.Text = string.Empty;
txtCity.Text = string.Empty;
txtZip.Text = string.Empty;
FillCountryComboBox();
cmbCountry.SelectedIndex = (int)Countries.Sverige;
m_closeForm = true;
}private void FillCountryComboBox()
{
cmbCountry.Items.AddRange(Enum.GetNames(typeof(Countries)));
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (m_closeForm)
e.Cancel = false;
else
e.Cancel = true;
}public bool ReadInput()
{
m_contact.FirstName = txtFirstName.Text;
m_contact.LastName = txtLastName.Text;
m_contact.AddressData = ReadAddress();
m_contact.EmailData = ReadEmail();
m_contact.PhoneData = ReadPhone();
bool ok = m_contact.Validate();
if (ok)
{
return true;
}
else
{
MessageBox.Show("Please provide at least your firstname, lastname, phone number, email address, city and country");
return false;
}
}private Address ReadAddress()
{
Address m_address = new Address();
m_address.Street = txtStreet.Text;
m_address.City = txtCity.Text;
m_address.ZipCode = txtZip.Text;
m_address.Country = (Countries)cmbCountry.SelectedIndex;
return m_address;
}private Email ReadEmail()
{
Email m_email = new Email();
m_email.Work = txtEMailBus.Text;
m_email.Personal = txtEMailPr.Text;
return m_email;
}private Phone ReadPhone()
{
Phone m_phone = new Phone();
m_phone.Home = txtHomePhone.Text;
m_phone.Other = txtCellPhone.Text;
return m_phone;
}
private void btnOK_Click(object sender, EventArgs e)
{
if (ReadInput())
{
UpdateGUI();
m_closeForm = true;
this.Close();
}
else
m_closeForm = false;
}public void UpdateGUI()
{
string strOut = m_contact.ToString();
MessageBox.Show(strOut);
}
}
}
} Also, the Customer ToString method is called by the MainForm to display the message on a MessageBox. This is the method in the MainForm:
private void btnAdd_Click(object sender, EventArgs e)
{
ContactForm contForm = new ContactForm("Add a new customer");
Customer cust = new Customer();
int index = lstResults.SelectedIndex;
if (index != -1)
contForm.ContactData = customerMngr.GetCustomer(index).ContactData;
if (contForm.ShowDialog() == DialogResult.OK)
{
lstResults.Items.Clear();
if (contForm.ReadInput() && (!String.IsNullOrEmpty(cust.ToString()) && (cust != null)))
{
MessageBox.Show(cust.ToString());
}
}
Why is the m_contact in the Customer class null, and not the one in the ContactForm class? Sorry for the length of this post, I'm having a really hard time understanding. Thank you!