[英]Object reference not set to an instance of an object
我有以下代码 -
private static void convert()
{
string csv = File.ReadAllText("test.csv");
XDocument doc = ConvertCsvToXML(csv, new[] { "," });
doc.Save("update.xml");
XmlTextReader reader = new XmlTextReader("update.xml");
XmlDocument testDoc = new XmlDocument();
testDoc.Load(@"update.xml");
XDocument turnip = XDocument.Load("update.xml");
webservice.function[] test = new webservice.function[1];
webservice.function CallWebService = new webservice.function();
foreach(XElement el in turnip.Descendants("row"))
{
test[0].com = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "com").SingleOrDefault().Attribute("value").Value);
test[0].Centre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "Centre").SingleOrDefault().Attribute("value").Value;
test[0].CCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "CCentre").SingleOrDefault().Attribute("value").Value;
MessageBox.Show(test[0].person, "person");
MessageBox.Show(System.Convert.ToString(test[0].actually), "Actually");
MessageBox.Show(System.Convert.ToString(test[0].com), "Com");
CallWebService.updateFeedStatus(test);
}
它出现了错误--NullReferenceException未处理,表示对象引用未设置为对象的实例。 第一行test [0] .account发生错误。
我怎么能通过这个?
初始化数组不会初始化数组中的对象。 尝试添加下面的第二行(假设您要使用默认构造函数):
webservice.singleSummary[] test = new webservice.singleSummary[1];
test[0] = new webservice.singleSummary();
我猜你的xml有一个你需要在你的xname中包含所需元素的命名空间,但正如其他评论所提到的,没有完全公开就没有什么可以做的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.