c# - 你调用的对象是空的

标签 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);
        }

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

我怎样才能克服这个问题?

最佳答案

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

webservice.singleSummary[] test = new webservice.singleSummary[1];
test[0] = new webservice.singleSummary();

关于c# - 你调用的对象是空的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4660103/

相关文章:

.net - 是否存在类似于System.IO.Path.Combine()的XPath串联操作?

XML 验证 : "No Child Element Is Expected At This Point"

java - Tomcat 压力测试超时

android - 仅适用于 Android Studio 中设计预览选项卡的 xml View 的占位符

c# - App.xaml MVVM指示灯中的空引用错误

c# - 使用给定的起点和终点从 html 中分离出一段数据

c# - 在 ContextMenu 中访问 ViewModel/DataContext

web-services - OpenESB CASA : Multiple operations in single WSDL

java - 如何让我的简单 Java Web 服务正常工作?

c# - COM、STA 和多线程