简体   繁体   English

你调用的对象是空的

[英]Object reference not set to an instance of an object

I have the following code - 我有以下代码 -

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

It is coming up with the error of - NullReferenceException was unhandled, saying that the object reference not set to an instance of an object. 它出现了错误--NullReferenceException未处理,表示对象引用未设置为对象的实例。 The error occurs on the first line test[0].account. 第一行test [0] .account发生错误。

How can I get past this? 我怎么能通过这个?

Initializing an array does not initialize the objects in the array. 初始化数组不会初始化数组中的对象。 Try adding the second line below (assuming you want to use the default constructor): 尝试添加下面的第二行(假设您要使用默认构造函数):

webservice.singleSummary[] test = new webservice.singleSummary[1];
test[0] = new webservice.singleSummary();
  1. Put a debugger on the process. 在进程上放置一个调试器。
  2. Identify which line of code is generating the error. 确定生成错误的代码行。 (Assuming Visual Studio) (假设Visual Studio)
  3. Test the object references on that line one by one until you determine which one has the null reference. 逐个测试该行上的对象引用,直到您确定哪一个具有空引用。
  4. Put in a null check before the line to fix the problem. 在行之前进行空检查以解决问题。

我猜你的xml有一个你需要在你的xname中包含所需元素的命名空间,但正如其他评论所提到的,没有完全公开就没有什么可以做的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM