Object reference not set to an instance of an object.

Posted by Lambo on Stack Overflow See other posts from Stack Overflow or by Lambo
Published on 2011-01-11T16:44:02Z Indexed on 2011/01/11 16:53 UTC
Read the original article Hit count: 271

I have the following code -

private static void convert()
    {
        string csv = File.ReadAllText("test.csv");
        string year = "2008";
        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.singleSummary[] test = new webservice.singleSummary[1];
        webservice.FinanceFeed CallWebService = new webservice.FinanceFeed();

        foreach(XElement el in turnip.Descendants("row"))
        {
            test[0].account = el.Descendants("var").Where(x => (string)x.Attribute("name") == "account").SingleOrDefault().Attribute("value").Value;
            test[0].actual = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "actual").SingleOrDefault().Attribute("value").Value);
            test[0].commitment = System.Convert.ToInt32(el.Descendants("var").Where(x => (string)x.Attribute("name") == "commitment").SingleOrDefault().Attribute("value").Value);
            test[0].costCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "costCentre").SingleOrDefault().Attribute("value").Value;
            test[0].internalCostCentre = el.Descendants("var").Where(x => (string)x.Attribute("name") == "internalCostCentre").SingleOrDefault().Attribute("value").Value;

            MessageBox.Show(test[0].account, "Account");
            MessageBox.Show(System.Convert.ToString(test[0].actual), "Actual");
            MessageBox.Show(System.Convert.ToString(test[0].commitment), "Commitment");
            MessageBox.Show(test[0].costCentre, "Cost Centre");
            MessageBox.Show(test[0].internalCostCentre, "Internal Cost Centre");

            CallWebService.updateFeedStatus(test, year);
        }

It is coming up with the error of - NullReferenceException was unhandled, saying that the object reference not set to an instance of an object. The error occurs on the first line test[0].account.

How can I get past this?

© Stack Overflow or respective owner

Related posts about c#

Related posts about Xml