简体   繁体   中英

Deserialized Xml Object Loop Throws NullReferenceException

This is sort of a continuation of another question and all of the code related to this question can be found, Here . I am experiencing a strange NullReferenceException Error that I just can't figure out. I am trying to build a table from a Deserialized web response. When I go to iterate through the object items I hit a NRE. The weird thing is I tested my condition statement by its self and I am able to catch it. Here is my code:

    public string getExample()
    {
        DataTable dt = new DataTable();
        XmlSerializer serializer = new XmlSerializer(typeof(WeeklyJobs));
        WeeklyJobs jobs;
        string xml = @"<?xml version = ""1.0""?>"
            + @"<WeeklyJobs>"
            + @"<DailyJobs Date = ""02/03/2012""/>"
            + @"<DailyJobs Date = ""02/04/2012"" TotalJobs = ""2"">"
            + @"<Jobs>"
            + @"<Job JobName = ""Job Name"" Description = ""Description""/>"
            + @"<Job JobName = ""Job Name"" Description = ""Description""/>"
            + @"</Jobs>"
            + @"</DailyJobs>"
            + @"<DailyJobs Date = ""02/05/2012"" TotalJobs = ""1"">"
            + @"<Jobs>"
            + @"<Job JobName = ""Job Name"" Description = ""Description""/>"
            + @"</Jobs>"
            + @"</DailyJobs>"
            + @"<DailyJobs Date = ""02/06/2012"" TotalJobs = ""2"">"
            + @"<Jobs>"
            + @"<Job JobName = ""Job Name"" Description = ""Description""/>"
            + @"<Job JobName = ""Job Name"" Description = ""Description""/>"
            + @"</Jobs>"
            + @"</DailyJobs>"
            + @"<DailyJobs Date = ""02/07/2012""/>"
            + @"</WeeklyJobs>";

        // Create an XmlTextReader
        using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
        {
            jobs = (WeeklyJobs)serializer.Deserialize(reader);
        }

    // Create Table
        dt.Columns.Add("Date");
        dt.Columns.Add("JobName");
        dt.Columns.Add("Description");

        for (int i = 0; i < jobs.Items.Length; i++ )
        {
            DataRow dr;
            object[] rowItems = null;
            rowItems[0] = jobs.Items[i].Date;
            if(jobs.Items[i].Jobs == null || jobs.Items[i].Jobs.Length == 0) //NRE is thrown Here <--
            {
                rowItems[1] = "";
                rowItems[2] = "";
            }
            else
            {
                foreach (WeeklyJobsDailyJobsJobsJob job in jobs.Items[i].Jobs)
                {
                    rowItems[1] = job.JobName;
                    rowItems[2] = job.Description;
                }
            }

            dr = dt.NewRow();
            dr.ItemArray = rowItems;
            dt.Rows.Add(dr);
        }

        return dt.Rows.Count.ToString();
    }

Now here's the part that I can't figure out. When I comment out the Create Table code and add an if statement on the item I know is null, the condition handles it correctly. here's what I add after commenting out the Create Table code:

        if(jobs.Items[0].Jobs == null)
        {
            return "null";
        }
        else
        {
            return jobs.Items[0].Jobs.Length.ToString();
        }

And it returns "null". I am not sure what is going on. Maybe my for loop is not properly setup? Thanks for any help!

You know, I often see the debugger putting the execution point on the line after an exception, when it breaks.

Maybe this is your problem instead:

object[] rowItems = null;
rowItems[0] = jobs.Items[i].Date;

The NRE is thrown by the indexer call on a null array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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