反序列化的Xml对象循环引发NullReferenceException [英] Deserialized Xml Object Loop Throws NullReferenceException
问题描述
这是另一个问题以及与此问题相关的所有代码,此处。我遇到一个奇怪的NullReferenceException错误,我无法弄清楚。我正在尝试从反序列化的Web响应构建表。当我遍历对象项时,我遇到了一个NRE。奇怪的是我自己测试了条件声明,并且能够抓住它。这是我的代码:
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();
}
现在这是我不知道的部分。当我注释掉创建表代码并在我知道为null的项目上添加if语句时,该条件会正确处理它。这是我在注释掉创建表代码后添加的内容:
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();
}
它返回 null。我不确定发生了什么。也许我的for循环未正确设置?谢谢您的帮助!
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.
也许这是您的问题:
object[] rowItems = null;
rowItems[0] = jobs.Items[i].Date;
NRE是由索引器调用对空数组抛出的。
The NRE is thrown by the indexer call on a null array.
这篇关于反序列化的Xml对象循环引发NullReferenceException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!