You can create new instance of LineItem
first like below since LineItem
is a child class of Invoice
so you have to use Invoice.LineItem
to initialize the class
Invoice.LineItem lineItem1 = new Invoice.LineItem();
lineItem1.ItemCode = 'item1';
lineItem1.LineAmount = 10;
Invoice.LineItem lineItem2 = new Invoice.LineItem();
lineItem2.ItemCode = 'item2';
lineItem2.LineAmount = 20;
Invoice.LineItem lineItem3 = new Invoice.LineItem();
lineItem3.ItemCode = 'item3';
lineItem3.LineAmount = 30;
After that create a new instance of Invoice class and after that add into its variables type
and LineItem
Invoice newInvoice = new Invoice();
newInvoice.Type = 'Sample';
newInvoice.LineItems.add(lineItem1 );
newInvoice.LineItems.add(lineItem2 );
newInvoice.LineItems.add(lineItem3 );
Updates
You have receive an error because newInvoice.LineItem
is null at the starting point.
So before adding the item to the list you have initialize the like
Invoice newInvoice = new Invoice();
newInvoice.Type = 'Sample';
newInvoice.LineItems = new List<Invoice.LineItem>();
newInvoice.LineItem.add(lineItem1 );
newInvoice.LineItem.add(lineItem2 );
newInvoice.LineItem.add(lineItem3 );