我有以下代码-
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();
关于c# - 你调用的对象是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660103/