你调用的对象是空的

Ebi*_*ser 2 c# xml web-services object nullreferenceexception

我有以下代码 -

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);
        }
Run Code Online (Sandbox Code Playgroud)

它出现了错误 - NullReferenceException未处理,表示对象引用未设置为对象的实例.第一行test [0] .account发生错误.

我怎么能通过这个?

Rei*_*ica 6

初始化数组不会初始化数组中的对象.尝试添加下面的第二行(假设您要使用默认构造函数):

webservice.singleSummary[] test = new webservice.singleSummary[1];
test[0] = new webservice.singleSummary();
Run Code Online (Sandbox Code Playgroud)