c# - 如何解决错误“对象引用未设置为对象的实例。”
问题描述
这是我的代码
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:'对象引用未设置为对象的实例。'
任何建议我需要在我的应用程序中做哪些必要的更改以避免错误
错误来自 LineItems.Add(billLine);
例如。尝试添加
List<BillLineItem> BillLineItems = new List<BillLineItem>();
public void AddBillLine(BillLine billLine)
{
LineItems.Add(billLine);
}
解决方案
您收到null reference
异常的原因是由于Bill
课程中的这一行:
public List<BillLine> LineItems { get; set; }
它目前没有初始化列表。最简单的解决方法是:
public List<BillLine> LineItems { get; set; } = new List<BillLine>();
推荐阅读
- python - .seek 也不适用于重置包含字符串的 csv.reader,还有什么其他方法可以使用?
- graphicsmagick - 使用 graphicsmagick 将 wmf 转换为 jpg 产生黑色图像
- javascript - 基于 level_id 的 SocialEngine 4.x/Zend 框架的回显文本
- java - java通过System.in读取字符
- owl - 对象属性的 OWL 类表达式语法最小和最大基数
- android-studio - 加载程序中的错误:java.lang.NullPointerException:尝试调用接口方法“boolean android.database.Cursor.moveToFirst()”
- r - R - 未列出嵌套的日期列表
- r - 朴素贝叶斯 2 类分类,下标越界
- string - Python 3.6 字符串格式化
- .net-core - 无法在 Windows Server 2008 R2 中运行 dotnet core 2 控制台应用程序