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.