了解未处理NullReferenceException的原因

本文关键字:NullReferenceException 未处理 了解 | 更新日期: 2023-09-27 18:27:13

我是一个初学者,我正在用c#编写一个简单的Windows窗体程序(对我来说不是那么容易,但对一个专业的程序员来说),它的工作原理是这样的:一个窗体(ContactForm类)从用户那里获得输入(名字、姓氏、地址、电子邮件和电话n),并通过Properties将其存储到Contact类中的变量中。Contact类调用其他三个类(Address、Email和Phone)的属性和方法来存储和验证用户的输入。Contact类还包含一个重写ToString方法,该方法将包含用户输入的变量格式化为字符串。在这一点上,我尝试用ContactForm调用这个ToString方法,并在MessageBox中显示字符串,它运行得很好。但后来我尝试用另一个类Customer调用该方法,该类有一个Contact类的对象,结果是一场灾难。编译器不断为Contact类(m_Contact)的表示为null的对象重新保留NullReferenceException。这是代码:

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。验证方法:

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

为了更清楚起见,这是电子邮件。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);
    }
    }
}

}此外,Customer ToString方法由MainForm调用,以在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类中的那个?很抱歉这篇文章太长了,我很难理解。非常感谢。

了解未处理NullReferenceException的原因

首先,必须检查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;