【问题标题】:How can I resolve the error 'Object reference not set to an instance of an object.' [duplicate]如何解决错误“对象引用未设置为对象的实例。” [复制]
【发布时间】:2020-11-05 14:02:02
【问题描述】:
  • 这是我的代码

     public class Bill 
    {
       public int BillNumber { get; set; }
       public DateTime BillDate { get; set; }
       public List<BillLine> LineItems { get; set; }
    
    
    
     public void AddBillLine(BillLine billLine)
     {
         LineItems.Add(billLine);
     }
    
     public void RemoveBillLine(int SOMEID)
     {
         throw new NotImplementedException();
     }
    
     /// GetTotal returns the sum of (Cost * Quantity) for each line item
    
     public decimal GetTotal()
     {
         throw new NotImplementedException();
     }
    
     public void MergeBill(Bill sourceBill)
     {
         throw new NotImplementedException();
     }
    
     /// deep clone of the current bill (all fields and properties)
    
     public Bill Clone()
     {
         throw new NotImplementedException();
     }
    
     public override string ToString()
     {
         throw new NotImplementedException();
     }
    }
    
    
    
      public class BillLine
    {
     public int BillLineId { get; set; }
     public string Description { get; set; }
     public int Quantity { get; set; }
     public double Cost { get; set; }
     }
    
    
    
    
       public class Program
     {
        static void Main(string[] args)
     {
         Console.WriteLine("Billing app started....");
    
         CreateBillWithOneItem();
         CreateBillWithMultipleItemsAndQuantities();
         RemoveItem();
         MergeBill();
         CloneBill();
         BillToString();
     }
    
     private static void CreateBillWithOneItem()
     {
         var bill = new Bill();
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 1,
             Cost = 6.99,
             Quantity = 1,
             Description = "Apple"
         });
    
         Console.WriteLine(bill.GetTotal());
     }
    
     private static void CreateBillWithMultipleItemsAndQuantities()
     {
         var bill = new Bill();
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 1,
             Cost = 10.21,
             Quantity = 4,
             Description = "Banana"
         });
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 2,
             Cost = 5.21,
             Quantity = 1,
             Description = "Orange"
         });
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 3,
             Cost = 5.21,
             Quantity = 5,
             Description = "Pineapple"
         });
    
         Console.WriteLine(bill.GetTotal());
     }
    
     private static void RemoveItem()
     {
         var bill = new Bill();
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 1,
             Cost = 5.21,
             Quantity = 1,
             Description = "Orange"
         });
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 2,
             Cost = 10.99,
             Quantity = 4,
             Description = "Banana"
         });
    
         bill.RemoveBillLine(1);
         Console.WriteLine(bill.GetTotal());
     }
    
     private static void MergeBill()
     {
         var bill1 = new Bill();
    
         bill1.AddBillLine(new BillLine()
         {
             BillLineId = 1,
             Cost = 10.33,
             Quantity = 4,
             Description = "Banana"
         });
    
         var bill2 = new Bill();
    
         bill2.AddBillLine(new BillLine()
         {
             BillLineId = 2,
             Cost = 5.22,
             Quantity = 1,
             Description = "Orange"
         });
    
         bill2.AddBillLine(new BillLine()
         {
             BillLineId = 3,
             Cost = 6.27,
             Quantity = 3,
             Description = "Blueberries"
         });
    
         bill1.MergeBill(bill2);
         Console.WriteLine(bill1.GetTotal());
     }
    
     private static void CloneBill()
     {
         var bill = new Bill();
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 1,
             Cost = 6.99,
             Quantity = 1,
             Description = "Apple"
         });
    
         bill.AddBillLine(new BillLine()
         {
             BillLineId = 2,
             Cost = 6.27,
             Quantity = 3,
             Description = "Blueberries"
         });
    
         var clonedBill = bill.Clone();
         Console.WriteLine(clonedBill.GetTotal());
     }
    
     private static void BillToString()
     {
         var bill = new Bill()
         {
             BillDate = DateTime.Now,
             BillNumber = 1000,
             LineItems = new List<BillLine>()
             {
                 new BillLine()
                 {
                     BillLineId = 1,
                     Cost = 6.99,
                     Quantity = 1,
                     Description = "Apple"
                 }
             }
         };
    
         Console.WriteLine(bill.ToString());
     }
    

    }

  • 谁能建议如何解决这个错误:System.NullReferenceException: 'Object reference not set to an instance of an object.'

  • 任何建议我需要在我的应用程序中做哪些必要的更改以避免错误

  • 错误来自 LineItems.Add(billLine);

例如。尝试添加

List<BillLineItem> BillLineItems = new List<BillLineItem>();

public void AddBillLine(BillLine billLine)
    {
        LineItems.Add(billLine);
    }

【问题讨论】:

  • LineItems 空。你需要有List&lt;BillLineItem&gt; LineItems = new List&lt;BillLineItem&gt;();

标签: c# .net .net-core c#-4.0 console-application


【解决方案1】:

您收到null reference 异常的原因是Bill 类中的这一行:

public List&lt;BillLine&gt; LineItems { get; set; }

目前没有初始化列表。最简单的解决方法是:

public List&lt;BillLine&gt; LineItems { get; set; } = new List&lt;BillLine&gt;();

【讨论】:

    【解决方案2】:

    在程序开始时,您调用方法“CreateBillWithOneItem”。然后,此方法创建一个新的 Bill 对象并调用方法“AddBillLine”。 'AddBillLine' 接受变量 LineItems 并尝试添加一个新的 BillLine 对象。

    到目前为止,您还没有通过将 LineItems 设置为对象来实例化它。因此,到目前为止它是空的。尝试在空对象上调用方法时,您将收到该错误。

    确实在 'BillToString' 中通过将其设置为该类型的新对象来实例化它,但这会在程序中稍后发生。

    【讨论】:

      【解决方案3】:

      除了@Hayden 所说的,您还可以为初始化 LineItems 的 Bill 类创建一个构造函数。由于您没有在 Bill 中指定构造函数,因此您调用的是默认构造函数,

      var bill = new Bill();
      

      尝试将构造函数添加到初始化 LineItems 的 Bill 类,

      public Bill()
      {
          LineItems = new List<BillLine>();
      }
      

      【讨论】:

        猜你喜欢
        • 2013-12-07
        • 2017-02-24
        • 1970-01-01
        • 1970-01-01
        • 2013-12-26
        • 2023-01-30
        • 2011-07-09
        • 2012-08-31
        • 2011-11-21
        相关资源
        最近更新 更多