List 为什么Add值就报错?

nemocc 2015-11-08 10:59:07
总是一到 bill.item.Add(billitem);这里就报“未将对象引用设置到对象的实例”。
代码如下:
public class StoreBill
{
/// <summary>
/// ID
///// </summary>
public int ID { get; set; }
/// <summary>
/// 商品集合
/// </summary>
private List<StoreBillItem> _Items = new List<StoreBillItem>();
public List<StoreBillItem> item { get; set; }
}

谢谢各位了!


Goods gd = new Goods();
StoreBill bill = new StoreBill();
StoreBillItem billitem = new StoreBillItem();

for (int i = 0; i < gridView1.RowCount; i++)
{

gd.ID = int.Parse(i.ToString() + "123");
gd.Code = gridView1.GetRowCellValue(i, "Code").ToString();
gd.Name = gridView1.GetRowCellValue(i, "Name").ToString();
gd.Scole = int.Parse(gridView1.GetRowCellValue(i, "Scole").ToString());
gd.Price = decimal.Parse(gridView1.GetRowCellValue(i, "Price").ToString());



billitem.ID = int.Parse(i.ToString() + "456");
billitem.Code = gridView1.GetRowCellValue(i, "Code").ToString();
billitem.Name = gridView1.GetRowCellValue(i, "Name").ToString();
billitem.Scole = int.Parse(gridView1.GetRowCellValue(i, "Scole").ToString());
billitem.Price = decimal.Parse(gridView1.GetRowCellValue(i, "Price").ToString());
bill.item.Add(billitem);
}
...全文
240 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
lc_ant 2015-11-09
  • 打赏
  • 举报
回复
private List<StoreBillItem> _Items = new List<StoreBillItem>(); public List<StoreBillItem> item { get{return _Items;} }
nemocc 2015-11-09
  • 打赏
  • 举报
回复
引用 5 楼 wjq 的回复:
private List<StoreBillItem> _Items = new List<StoreBillItem>(); public List<StoreBillItem> item { get{return _Items;} }
及时结帖!谢谢了!
wjq 2015-11-09
  • 打赏
  • 举报
回复
private List<StoreBillItem> _Items = new List<StoreBillItem>(); public List<StoreBillItem> item { get{return _Items;} }
nemocc 2015-11-08
  • 打赏
  • 举报
回复
引用 3 楼 starfd 的回复:
你的item又没用那个带下划线的私有字段,那个倒是new了的,然后get set的呢没有实例化,当然报异常了
谢谢版主大人,我忘记贴代码了, public class StoreBillItem { public int ID { get; set; } /// <summary> /// 商品编码 /// </summary> public string Code { get; set; } public string Name { get; set; } public int Scole { get; set; } public decimal Price { get; set; }
  • 打赏
  • 举报
回复
你的item又没用那个带下划线的私有字段,那个倒是new了的,然后get set的呢没有实例化,当然报异常了
nemocc 2015-11-08
  • 打赏
  • 举报
回复
引用 1 楼 slyzly 的回复:
for (int i = 0; i < gridView1.RowCount; i++) { StoreBillItem billitem = new StoreBillItem();
放在里面吗?
事理 2015-11-08
  • 打赏
  • 举报
回复
for (int i = 0; i < gridView1.RowCount; i++) { StoreBillItem billitem = new StoreBillItem();

110,536

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧