[英]Using SerialPort in second form
I am trying to write to a .xml file and am getting the error Object reference not set to an instance of an object. 我正在尝试写入.xml文件,并且收到错误“ 对象引用未设置为对象的实例”。 on line 49 (CreateNode) . 在第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();
}
Dim mySerialPort as serialPort
mySerialPort.PortName = SetPortName(mySerialPort.ApplicationPort);
MSDN is a great resource . MSDN是一个很好的资源 。
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. 我看不到那里会有XmlTextwriter。 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. 创建subs,function等时,不需要将所有变量都填充到顶部的声明中。 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. 我敦促您使用MSDN并对此进行更多研究,它将变得更加容易。 Here is a link to the MSDN .Net Framework Class Library - it covers EVERYTHING :) 这是MSDN .Net Framework类库的链接-它涵盖了所有内容:)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.