Reputation: 1155
I am trying to write to a .xml file and am getting the error Object reference not set to an instance of an object. on line 49 (CreateNode). This is the code that I have tried, but no luck.
private void CreateNode(string Port, string BaudRate, string DataBits, string Parity,_
string StopBits, string Handshaking, XmlTextWriter writer)
{
//Writing to the .xml file. This will make the program be able to load the properties last used.
writer.WriteStartElement("ApplicationProperties");
writer.WriteStartElement("Port");
writer.WriteString(Port);
writer.WriteEndElement();
writer.WriteStartElement("BaudRate");
writer.WriteString(BaudRate);
writer.WriteEndElement();
writer.WriteStartElement("DataBits");
writer.WriteString(DataBits);
writer.WriteEndElement();
writer.WriteStartElement("Parity");
writer.WriteString(Parity);
writer.WriteEndElement();
writer.WriteStartElement("StopBits");
writer.WriteString(StopBits);
writer.WriteEndElement();
writer.WriteStartElement("Handshaking");
writer.WriteString(Handshaking);
writer.WriteEndElement();
writer.WriteEndElement();
}
private void SaveProperties()
{
//CreateNode(everything being referenced. Put text boxes, and drop down boxes here.
XmlTextWriter writer = new XmlTextWriter(@"C:\ForteSenderv2.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(ApplicationPort.PortName, ApplicationPort.BaudRate.ToString(), ApplicationPort.DataBits.ToString(), ApplicationPort.Parity.ToString(), ApplicationPort.StopBits.ToString(), ApplicationPort.Handshake.ToString(), writer);
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Close();
}
Upvotes: 1
Views: 156
Reputation:
Dim mySerialPort as serialPort
mySerialPort.PortName = SetPortName(mySerialPort.ApplicationPort);
Edit
error: Object reference not set to an instance of an object. on line 49 (CreateNode).
private void CreateNode(string Port, string BaudRate, string DataBits, string Parity,_
string StopBits, string Handshaking, XmlTextWriter writer)
private void blabla (PortName as string, BaudRate as string, DataBits as string,_
Parity as string, Handshaking as string)
I cannot see how you would have XmlTextwriter in there. Personally, I have not seen anything quite like this.. I can see your efforts and really appreciate your work and what you are trying to achieve, I truly think you are overcomplicating things. The trick with coding (not that I am an expert), is to go for the simplest way to create the functionality you require. As short as possible, as long as necessary.
I think you need to rethink your algorithm of what you are trying to achieve, Am I going about this in the simplest way or am I making unnecessary complications to this project?
When creating subs, functions, etc, you don't need to stuff all the variables into the declaration at the top. That is what the body is for.
I urge you to use the MSDN and study this a little more, and it will get easier. Here is a link to the MSDN .Net Framework Class Library- it covers EVERYTHING :)
Upvotes: 2