反序列化的Xml对象循环抛出NullReferenceException[英] Deserialized Xml Object Loop Throws NullReferenceException

本文是小编为大家收集整理的关于反序列化的Xml对象循环抛出NullReferenceException的处理/解决方法,可以参考本文帮助大家快速定位并解决问题,中文翻译不准确的可切换到English标签页查看源文。

问题描述

本文来自:IT宝库(https://www.itbaoku.cn)

这有点像另一个问题可以找到与此问题相关的所有代码,在这里.我正在遇到一个奇怪的NullReferenceException错误,我根本不知道.我正在尝试从避免的Web响应中构建一个表.当我迭代对象项目时,我点击了NRE.奇怪的是,我通过自我测试了我的状况陈述,我能够抓住它.这是我的代码:

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

现在这是我不知道的部分.当我评论创建表代码并在我知道的项目上添加一个if语句时,条件会正确处理.这是我在评论创建表代码后添加的内容:

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

它返回" null".我不确定发生了什么.也许我的循环无法正确设置?感谢您的帮助!

推荐答案

您知道,我经常看到调试器将执行点放在线上 一个例外.

也许这是您的问题:

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

NRE由零阵列上的索引呼叫抛出.

本文地址:https://www.itbaoku.cn/post/2043800.html

问题描述

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.