2015-10-28 38 views
0

這是一個經典問題,但我看不出如何在代碼中找出它。C#古典錯誤。對象需要實例化

未將對象引用設置爲對象的實例。

我知道我必須寫一些像new IList<SubInvoice>();但我看不到在哪裏。

private void GetSub() 
{ 
    var invoice = Selected; 
    var invs = (from i in ctx.Invoices 
       join p in ctx.products on i.refSupp equals p.refsup 
       join u in ctx.units on p.unit equals u.idunit 
       where i.suppInvNumber == invoice.suppInvNumber 
       select new SubInvoice 
       { 
        Invoice = i, 
        description = p.description, 
        unit1 = u.unit1 
       }); 

    if(Products != null) 
    Products.Clear(); 

    if (invs != null) 
     foreach(var inv in invs) 
     { 
      SubInvoice sub = new SubInvoice(){Invoice=inv.Invoice, description = inv.description, unit1 = inv.unit1}; 
      Products.Add(sub); //error 
     } 

} 

private IList<SubInvoice> _products; 

public IList<SubInvoice> Products 
{ 
    get 
    { 
     return _products; 
    } 
    set 
    { 
     _products = value; 
     OnPropertyChanged("Products"); 
    } 
} 

public class SubInvoice 
{ 
    public Invoice Invoice { get; set; } 
    public string description { get; set; } 
    public string unit1 { get; set; } 
} 
+0

你在哪裏得到這個錯誤 –

+0

Products.Add(分); – Cantinou

+0

@ M.kazemAkhgary,產品不移動,因此不需要減速。哦,你的意思是**聲明**。 :-P – GreatAndPowerfulOz

回答

3

以下替換:

if(Products != null) 
    Products.Clear(); 

if(Products != null) 
    Products.Clear(); 
else 
    Products = new List<SubInvoice>(); 
3
private IList<SubInvoice> _products = new List<SubInvoice>()