了解为什么NullReferenceException未处理?

时间:2015-12-16 17:06:50

标签: c# nullreferenceexception

我是初学者,我在c#编写一个简单的Windows窗体程序(对我来说并不容易,但对于专业程序员),其工作方式如下:表单(ContactForm类)获取输入(来自用户的名字,姓氏,地址,电子邮件和电话号码)并通过“属性”将其存储在Contact类中的变量中。 Contact类调用其他三个类(地址,电子邮件和电话)中的属性和方法来存储和验证用户的输入。 Contact类还包含一个覆盖ToString方法,该方法将包含用户输入的变量格式化为字符串。 此时,我尝试使用ContactForm调用此ToString方法并在MessageBox中显示字符串,并且它工作正常。但后来我尝试用另一个类Customer调用该方法,该类有一个Contact类的对象,这是一场灾难。编译器继续将Contact类(m_contact)的对象的NullReferenceException与null相关联。这是代码:

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;
    }
}

} 星号中的行是发现错误的行。这个是m_contact.Validate方法:

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;
    }

为了更清晰,这是email.CheckEmail方法,但上面的检查方法肯定不是问题:

public bool CheckEmail()
    {  
        bool ok = false;
        if ((!string.IsNullOrEmpty(m_personal)) || (!string.IsNullOrEmpty(m_work)))
            ok = true;
        else
            ok = false;
        return ok;
    }

这是ContactForm类中显示Contact类的ToString方法的代码,完全没问题:

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);
    }
    }
}

} 此外,MainForm调用Customer ToString方法以在MessageBox上显示消息。这是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());
            }
        }

为什么Customer类中的m_contact为null,而不是ContactForm类中的m_contact?对不起这篇文章的篇幅,我很难理解。谢谢!

1 个答案:

答案 0 :(得分:0)

首先,您必须检查m_contact是否为空,然后尝试在该对象上调用方法ToString。否则你得到NullReferenceException,因为你在null对象上调用方法。

基本上,重写

 **if ((!string.IsNullOrEmpty(m_contact.ToString())) && (m_contact != null) && (m_contact.Validate()))**
        {
            strOut = m_contact.ToString();
        }
        else
            strOut = null;

为:

 if (m_contact != null && !string.IsNullOrEmpty(m_contact.ToString()) && m_contact.Validate())
 {
     strOut = m_contact.ToString();
 }
 else
     strOut = null;
相关问题