-3

I am getting the error message

Object not set to an instance of an object.

Now I understand that this error message means that I am calling a object that is holding the value of null, and I think I know where this is happening in my code, I just need to know how to fix it. Inside form1(MainBox)'s class I have made a reference to form2(ApplicationProperties). Inside form1, I am trying to write to a .xml file with the values from combo boxes on form2. This is where the error message is firing.

At

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().

  } 

In form2(ApplicationProperties), I am clicking a button, which should write to the .xml file.

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

At the top of the event handler, I am making a new instance of the first form, so I can call back to the CreateNode funciton in form1.

So I believe that it is the way I am going about referencing and making instances between the forms that is giving me a null value. I was wondering what I can do to fix this and carry the value over to the other form, and be able to write it to the .xml file.

4

1 回答 1

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 中;但是,在这种情况下,有很多数据,我不知道它到底是什么。

父母通过使用属性查询孩子的状态来响应孩子的活动。

于 2013-08-08T19:01:12.433 回答