0

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!

Community
  • 1
  • 1
waltmagic
  • 631
  • 2
  • 9
  • 22
  • 2
    Why don't you simply use `DataSet ds = new DataSet(); ds.ReadXml(new StringReader(xml));` – L.B Jun 06 '14 at 16:42
  • Do you really have a class called `WeeklyJobsDailyJobsJobsJob`? Sorry, it stuck out like a sore thumb. – TyCobb Jun 06 '14 at 16:45
  • I agree...I used xsd.exe to create a class for me with some xml. It looks terrible – waltmagic Jun 06 '14 at 16:47
  • @L.B I initially tried to use DataSet but was having problems aligning the jobs by their date because the XML response I'm given creates separate classes for WeeklyJobs, DailyJobs, Jobs and Job. It was recommended that I try XmlSerializer and sense I had not used this class before I decided to learn how to use it. I know the code is noobish and that is because I am new to this :) – waltmagic Jun 06 '14 at 16:51
  • @L.B I was the one who recommended XmlSerializer, though if I had known waltmagic was then going to copy the data into a dataset, I would have suggested figuring out how to get the dataset to work properly the way he wants it to. I meant to suggest XmlSerializer as an alternative way to get the data into memory. – phoog Jun 06 '14 at 16:57
  • possible duplicate of [What is a NullReferenceException and how do I fix it?](http://stackoverflow.com/questions/4660142/what-is-a-nullreferenceexception-and-how-do-i-fix-it) – Daniel Kelley Jun 06 '14 at 17:13
  • @L.B Got this working using DataSet as well! Thanks everyone for your feedback! – waltmagic Jun 06 '14 at 18:20

1 Answers1

2

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.

phoog
  • 42,068
  • 6
  • 79
  • 117
  • object[] rowItems = new object[dt.Columns.Count]; cleared that NRE right up. I feel silly, jeez...Thanks again! – waltmagic Jun 06 '14 at 17:02
  • @waltmagic we all do silly things like that from time to time. The debugger's indicating the wrong line for the exception doesn't help at all. Had it indicated the correct line, you would have solved the problem in 10 seconds. – phoog Jun 06 '14 at 17:15