Silverlight 添加到列表抛出对象引用未设置到对象实例
我在 IE 8 上使用 Silverlight 4。我为 SharePoint 2010 创建了一个新的 Silverlight Web 部件,用于读取 2 种外部内容类型。一个代表来自 Code Plex 的 Chinook 数据库中的发票,另一个代表每张发票的行。我正在尝试使用 Telerik Rad GridView 控件在主从布局中显示代码。我以几种不同的方式尝试了这段代码。以下是一般描述: 我有 2 个对象 InvoiceHeader 和 InvoiceLine。
发票标头包含 InvoiceLines 列表
public List<InvoiceLine> lines { get; set; }
我能够使其“工作”的唯一方法是使用 foreach 并循环返回的发票,然后使用子 foreach(var unavailable in returnedInvoices)
和将每一行添加到列表中,然后使用 invoice.lines.Add(new InvoiceLine(line));
将列表添加到当前发票中
当然,问题是我无法使用 清除行>lines.Clear()
或所有 InvoiceHeader 对象中的所有行均被清除。由于我无法清除列表,每个发票都将以下发票行添加到其自己的行中。因此,我创建了一个 InvoiceHeaders 跟踪数组,并尝试使用它来执行 tracking[x].lines.Add(new InvoiceLine(line));
然后我在这篇文章的主题中收到了错误。尝试将 InvoiceLine 添加到 InvoiceHeader 对象的列表时,未将对象引用设置为对象的实例。我并不是真正的开发人员,所以我非常感谢您能给我的任何帮助。目前代码已经变得疯狂,所以我对此表示歉意。我在下面包含了 successCallback:
void succeededCallback(object sender, ClientRequestSucceededEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
invoices.Clear();
invoiceLines.Clear();
int x = 0;
int y = 0;
foreach (var inv in returnedInvoices)
{
x++;
}
InvoiceHeader[] tracker = new InvoiceHeader[x];
foreach (var invoice in returnedInvoices)
{
tracker[y] = new InvoiceHeader(invoice);
//Get the lines that correspond to this invoice.
foreach (var line in returnedLines)
{
if(line.FieldValues["InvoiceId"].ToString() == invoice.FieldValues["InvoiceId"].ToString())
{
//tracker[y].lines.Add(new InvoiceLine(line));
/*
* If I comment out the line above
* I do not get the error, but I also
* don't get my detail level.
*/
}
}
invoices.Add(tracker[y]);
if (y < x) { y++; }
}
radGridView1234.ItemsSource = invoices;
}
);
}
I'm using Silverlight 4 on IE 8. I have created a new Silverlight web part for SharePoint 2010 that I am using to read 2 external content types. One represents the Invoices in the Chinook database from Code Plex and the other is the lines of each invoice. I am attempting to display the code in a Master-Detail layout using the Telerik Rad GridView control. I have tried this code in several different ways. Here is a general description:
I have 2 objects InvoiceHeader and InvoiceLine.
Invoice header contains a list of InvoiceLines
public List<InvoiceLine> lines { get; set; }
The only way I was able to get this to "work" was to use a foreach and cycle through the returnedInvoices and then use a sub foreach(var invoice in returnedInvoices)
and add each line to a list and then adding the list to the current invoice using invoice.lines.Add(new InvoiceLine(line));
Of course the problem was that I could not clear lines using lines.Clear()
or all of the lines were cleared in all of the InvoiceHeader objects. Since I could not clear the list each invoice had the following invoices lines added to its own lines. So I created a tracking array of InvoiceHeaders and tried using it to do tracking[x].lines.Add(new InvoiceLine(line));
and I then received the error in my subject for this post. Object reference not set to an instance of an object when trying to add an InvoiceLine to the InvoiceHeader object's List. I'm not really a developer, so I would appreciate any help you can give me. Currently the code has become crazy, so my apologies for that. I have included the succeededCallback below:
void succeededCallback(object sender, ClientRequestSucceededEventArgs e)
{
this.Dispatcher.BeginInvoke(() =>
{
invoices.Clear();
invoiceLines.Clear();
int x = 0;
int y = 0;
foreach (var inv in returnedInvoices)
{
x++;
}
InvoiceHeader[] tracker = new InvoiceHeader[x];
foreach (var invoice in returnedInvoices)
{
tracker[y] = new InvoiceHeader(invoice);
//Get the lines that correspond to this invoice.
foreach (var line in returnedLines)
{
if(line.FieldValues["InvoiceId"].ToString() == invoice.FieldValues["InvoiceId"].ToString())
{
//tracker[y].lines.Add(new InvoiceLine(line));
/*
* If I comment out the line above
* I do not get the error, but I also
* don't get my detail level.
*/
}
}
invoices.Add(tracker[y]);
if (y < x) { y++; }
}
radGridView1234.ItemsSource = invoices;
}
);
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你什么时候初始化
lines
?通常,集合属性在用作自动属性时只有一个 getter 和一个私有 setter。您必须初始化它,因为您当前正在对 null 调用
.Add
。When are you initializing
lines
? Typically, collection properties only have a getter with a private setter when used as an autoproperty.You must initialize it as you're currently calling
.Add
on a null.我在代码示例中看不到对
lines
的引用,我无法真正理解它,但此异常意味着您有一个空引用。像行声明这样的自动属性具有类型默认值的初始值,这对于列表意味着 null,因此繁荣。不建议直接通过属性公开列表,因为您得到的行为很少与您的预期相符,我建议您将其重构为封装私有和初始化字段的非自动属性,即:
但对于现在的这个问题,最简单的事情就是在尝试添加之前的某个时刻设置
lines = new List();
- 在构造函数中根据偏好进行添加。I can't see a reference to
lines
in your code sample which I couldn't really follow, but this exception means you have a null reference. Auto properties like your lines declaration have an initial value of the default for the type, which for a List means null, hence boom.It's not recommended to expose Lists directly through a property because the behaviour you'll get rarely matches what you intended, and I would suggest you refactor that to a non-autoprop encapsulating a private and initialised field, i.e.:
But for this problem right now, the simplest thing to do is just to set
lines = new List<InvoiceLine>();
at some point before you try and add to it - in the constructor from preference.