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

Mark
  • 3,273
  • 2
  • 36
  • 54
Chase Ernst
  • 1,147
  • 1
  • 21
  • 53
  • 2
    Almost all cases of `NullReferenceException` are the same. Please see "[What is a NullReferenceException in .NET?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-in-net)" for some hints. – John Saunders Aug 08 '13 at 18:59
  • 2
    I would suggest not having one form calling another's methods like this... raise an event and pass a com port configuration struct instead. – J... Aug 08 '13 at 19:01

1 Answers1

3

Looks like your SelectedItem is null. Set the breakpoint on that line and inspect the SelectedItem property to see if it is null.

As an aside, the pattern you are using to do this is not a recommended pattern. You should pass information between forms via events, callbacks, return values, etc; rather than reaching into one form from another.

Edit per comment #1

Bear in mind that I don't have your actual code, so this is a very rough example...

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

Now there's no circular references. The parent form holds a reference to it's child, but the child knows nothing of the parent.

The parent is notified of the child's activity through an event. Normally I would wrap all the data necessary to respond to the event into the EventArgs; but, in this case, there's a lot of data, and I don't know what exactly it is.

The parent responds to the child's activity by querying the child for it's state via usage of properties.

John Kraft
  • 6,811
  • 4
  • 37
  • 53
  • SelectedItem is null, I was just wondering how I would be able to make the forms co-operate how I have them set up now. – Chase Ernst Aug 08 '13 at 19:08