对象未设置为对象错误的实例

时间:2013-08-08 18:56:04

标签: c# xml winforms nullreferenceexception

我收到错误消息

  

对象未设置为对象的实例。

现在我明白这个错误消息意味着我正在调用一个持有null值的对象,我想我知道在我的代码中发生了什么,我只需要知道如何修复它。在form1(MainBox)的类中,我引用了form2(ApplicationProperties)。在form1中,我试图用form2上的组合框的值写入.xml文件。这是错误消息触发的位置。

CreateNode(ApplicationPropertiesWindow.Portbx.SelectedItem.ToString(),.........

public partial class MainBox : Form
{
    //Making a refernce of Form2 called 'form2'.
    ApplicationProperties ApplicationPropertiesWindow = new ApplicationProperties();
       public void SaveApplicationProperties()
    {
        try
        {
            //CreateNode(everything being referenced. Put text boxes, and drop down boxes here.
            XmlTextWriter writer = new XmlTextWriter(@"C:\ForteSenderv3.0\Properties.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            //Making the code indeted by 2 characters.
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            //Making the start element "Table".
            writer.WriteStartElement("Forte_Data_Gatherer_Application");
            //Calling the rst of the .xml file to write.
            CreateNode(ApplicationPropertiesWindow.Portbx.SelectedItem.ToString(), ApplicationPropertiesWindow.BaudRatebx.SelectedItem.ToString(), ApplicationPropertiesWindow.DataBitsbx.SelectedItem.ToString(), ApplicationPropertiesWindow.Paritybx.SelectedItem.ToString(), ApplicationPropertiesWindow.StopBitsbx.SelectedItem.ToString(), ApplicationPropertiesWindow.Handshakingbx.SelectedItem.ToString(), writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Writing to .xml file failure: " + ex.Message);
        }
    }//End SaveApplicationProperties().

  } 

在form2(ApplicationProperties)中,我点击一个按钮,该按钮应该写入.xml文件。

    private void CommPortAcceptbtn_Click(object sender, EventArgs e)
    {
        //Making an instance of MainBox.
        var MainBoxWindow = new MainBox();
        //Need to set text boxes to change the values of the comm port.
        //PropertiesHaveChanged();
        MainBoxWindow.SaveApplicationProperties();
        //Hiding the window, because closing it makes the window unaccessible.
        this.Hide();
        this.Parent = null;
    }

在事件处理程序的顶部,我正在创建第一个表单的新实例,因此我可以回调form1中的CreateNode函数。

所以我相信这是我在表单之间引用和制作实例的方式,它给了我一个空值。我想知道我能做些什么来解决这个问题,并将值传递给另一个表单,并能够将其写入.xml文件。

1 个答案:

答案 0 :(得分:3)

看起来您的SelectedItem为空。在该行上设置断点并检查SelectedItem属性以查看它是否为空。

另外,您使用的模式不是推荐的模式。您应该通过事件,回调,返回值等在表单之间传递信息;而不是从另一种形式达成一种形式。

根据评论编辑#1

请记住我没有你的实际代码,所以这是非常粗略的例子......

public partial class ApplicationProperties : Form
{
    public event EventHandler SaveRequested;

    public SomeObjectType Portbx        {get;set;}
    public SomeObjectType BaudRatebx    {get;set;}
    public SomeObjectType DataBitsbx    {get;set;}
    public SomeObjectType Paritybx      {get;set;}
    public SomeObjectType StopBitsbx    {get;set;}
    public SomeObjectType Handshakingbx { get; set; }

    public ApplicationProperties()
    {
        InitializeComponent();
    }

    private void CommPortAcceptbtn_Click(object sender, EventArgs e)
    {
        if (SaveRequested != null)
            SaveRequested(this, new EventArgs());
    }
}

public partial class MainBox : Form
{
    ApplicationProperties ApplicationPropertiesWindow;
    public MainBox()
    {
        InitializeComponent();
        ApplicationPropertiesWindow = new ApplicationProperties();
        ApplicationPropertiesWindow.SaveRequested += ApplicationPropertiesWindow_SaveRequested;
    }

    void ApplicationPropertiesWindow_SaveRequested(object sender, EventArgs e)
    {
        ApplicationPropertiesWindow.Hide();
        SaveApplicationProperties();
    }

    public void SaveApplicationProperties()
    {

        try
        {
            //CreateNode(everything being referenced. Put text boxes, and drop down boxes here.
            XmlTextWriter writer = new XmlTextWriter(@"C:\ForteSenderv3.0\Properties.xml", System.Text.Encoding.UTF8);
            writer.WriteStartDocument(true);
            //Making the code indeted by 2 characters.
            writer.Formatting = Formatting.Indented;
            writer.Indentation = 2;
            //Making the start element "Table".
            writer.WriteStartElement("Forte_Data_Gatherer_Application");
            //Calling the rst of the .xml file to write.
            CreateNode(
                ApplicationPropertiesWindow.Portbx.SelectedItem.ToString(), 
                ApplicationPropertiesWindow.BaudRatebx.SelectedItem.ToString(), 
                ApplicationPropertiesWindow.DataBitsbx.SelectedItem.ToString(), 
                ApplicationPropertiesWindow.Paritybx.SelectedItem.ToString(), 
                ApplicationPropertiesWindow.StopBitsbx.SelectedItem.ToString(), 
                ApplicationPropertiesWindow.Handshakingbx.SelectedItem.ToString(), 
                writer);
            writer.WriteEndElement();
            writer.WriteEndDocument();
            writer.Close();
        }
        catch (Exception ex)
        {
            MessageBox.Show("Writing to .xml file failure: " + ex.Message);
        }
    }
}

现在没有循环引用。父表单包含对其子项的引用,但是孩子对父项一无所知。

通过活动通知家长孩子的活动。通常我会将响应事件所需的所有数据包装到EventArgs中;但是,在这种情况下,有很多数据,我不知道究竟是什么。

父母通过使用财产查询孩子的状态来回应孩子的活动。