簡體   English   中英

你調用的對象是空的

[英]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();
  1. 在進程上放置一個調試器。
  2. 確定生成錯誤的代碼行。 (假設Visual Studio)
  3. 逐個測試該行上的對象引用,直到您確定哪一個具有空引用。
  4. 在行之前進行空檢查以解決問題。

我猜你的xml有一個你需要在你的xname中包含所需元素的命名空間,但正如其他評論所提到的,沒有完全公開就沒有什么可以做的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM