-3
  • Here is my code

     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());
     }
    

    }

  • Can anyone please suggest how to solve this error : System.NullReferenceException: 'Object reference not set to an instance of an object.'

  • Any suggestion what are the necessary changes I need to do in my application to avoid the errors

  • The error is coming from LineItems.Add(billLine);

eg. tried adding

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

public void AddBillLine(BillLine billLine)
    {
        LineItems.Add(billLine);
    }
Dave
  • 25
  • 5

3 Answers3

1

The reason that you're receiving a null reference exception is due to this line in the Bill class:

public List<BillLine> LineItems { get; set; }

It's currently not initializing the list. The simplest way to resolve would be to do:

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

Hayden
  • 2,902
  • 2
  • 15
  • 29
0

At the start of your program you call the method 'CreateBillWithOneItem'. This method then creates a new Bill object and calls the method 'AddBillLine'. 'AddBillLine' takes the variable LineItems and tries to to add a new BillLine object.

At no point up to here have you instantiated LineItems by setting it to an object. Therefore it is null up until this point. When attempting to call a method on a null object you will receive that error.

You do instantiate it in 'BillToString' by setting it to a new object of that type but that occurs later in the program.

Chris S
  • 61
  • 1
  • 6
0

In addition to what @Hayden said, you can also create a constructor for the Bill class where you initialize LineItems. Since you did not specify a constructor in Bill you are calling the default constructor,

var bill = new Bill();

Try adding a constructor to the Bill class that initializes LineItems,

public Bill()
{
    LineItems = new List<BillLine>();
}
David Kidwell
  • 600
  • 1
  • 6
  • 15