[英]Silverlight adding to List<T> throws Object reference not set to an instance of an object
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. 我在IE 8上使用Silverlight4。我为SharePoint 2010创建了一个新的Silverlight Web部件,该部件用于读取2种外部内容类型。 One represents the Invoices in the Chinook database from Code Plex and the other is the lines of each invoice.
一个代表Code Plex的Chinook数据库中的发票,另一个代表每张发票的行。 I am attempting to display the code in a Master-Detail layout using the Telerik Rad GridView control.
我正在尝试使用Telerik Rad GridView控件以“主从细节”布局显示代码。 I have tried this code in several different ways.
我以几种不同的方式尝试过此代码。 Here is a general description: I have 2 objects InvoiceHeader and InvoiceLine.
这里是一般描述:我有2个对象InvoiceHeader和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));
我能够使它“工作”的唯一方法是使用foreach并循环遍历returnInvoices,然后使用sub
foreach(var invoice in returnedInvoices)
并将每一行添加到列表中,然后将列表添加到当前列表中发票使用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. 当然,问题在于我无法使用
lines.Clear()
清除行,或者在所有InvoiceHeader对象中清除了所有行。 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));
因此,我创建了一个InvoiceHeaders跟踪数组,并尝试使用它来进行
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.
尝试将InvoiceLine添加到InvoiceHeader对象的列表时,对象引用未设置为对象的实例。 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:
我在下面包括了successedCallback:
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;
}
);
}
When are you initializing lines
? 您何时初始化
lines
? Typically, collection properties only have a getter with a private setter when used as an autoproperty. 通常,集合属性在用作自动属性时仅具有带有私有设置程序的getter。
public List<InvoiceLine> lines { get; private set; }
You must initialize it as you're currently calling .Add
on a null. 您必须在当前调用
.Add
对其进行初始化。
lines = new List<InvoiceLIne>();
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. 我看不到您的代码示例中对
lines
的引用,我无法真正遵循,但是此异常意味着您具有空引用。 Auto properties like your lines declaration have an initial value of the default for the type, which for a List means null, hence boom. 像您的行声明之类的自动属性的类型的初始值为默认值,对于List来说,它的值为null,因此是繁荣。
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, ie: 不建议直接通过属性公开List,因为您将获得的行为很少符合您的预期,并且我建议您将其重构为非自动封装的私有和初始化字段,即:
// private field for your data
private List<InvoiceLine> _lines = new List<InvoiceLine>();
// public prop to expose it - but I'd be better still if I just exposed an Add method.
public List<InvoiceLine> Lines
{
get { return this._lines; }
}
But for this problem right now, the simplest thing to do is just to set lines = new List<InvoiceLine>();
但是对于现在的问题,最简单的方法是设置
lines = new List<InvoiceLine>();
at some point before you try and add to it - in the constructor from preference. 在尝试将其添加之前的某个时候-从首选项的构造函数中添加。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.