Ok, I'm making a program where a customer can select any number of items from a list. Depending on the product and quantity the customer enters the values are added to a product and quantity array. When I run the program to calculate the subtotal I receive an error(java.lang.NullPointerException) under public void calcSubtotal() where it states sum += orderProduct[i].getPrice() * orderQuantity[i]; I can't figure out the reason for this error at all! Any help would be much appreciated!
CODE
package javazon;
import java.text.NumberFormat;
import java.util.Arrays;
public class Order {
//class variables
private Customer orderCustomer;
private Clerk orderClerk;
private Product[] orderProduct;
private int[] orderQuantity;
//store totals
private double subtotal;
private double tax;
private double total;
//constant defining tax
private static double TAX_RATE = 0.0825;
//keep track of how many products were added
private int productCount;
//constructor
public Order(){
orderProduct = new Product[1];
orderQuantity = new int[1];
}
//setter to assign customer
public void setOrderCustomer(Customer aCustomer){
orderCustomer = aCustomer;
}
//setter to assign clerk
public void setOrderClerk(Clerk aClerk){
orderClerk = aClerk;
}
public void setOrderProduct(Product aProduct, int aQty){
//TODO
Product[] tempOrderedProducts = new Product[orderProduct.length + 1];
for (int i = 0; i < orderProduct.length; i++) {
tempOrderedProducts[i] = orderProduct[i];
}
tempOrderedProducts[orderProduct.length] = aProduct;
int[] tempOrderedQuantity = new int[orderQuantity.length + 1];
for (int j = 0; j < orderQuantity.length; j++) {
tempOrderedQuantity[j] = orderQuantity[j];
}
tempOrderedQuantity[orderQuantity.length] = aQty;
//EACH TIME A USER ADDS A PRODUCT TO THE ORDER
//IF IT IS THE FIRST PRODUCT ADDED TO THE ORDER THEN
//STORE IT IN THE orderProduct ARRAY
//IF MORE PRODUCTS ARE ADDED, YOU HAVE TO RESIZE THE
//orderProduct and orderQuantity arrays.
//the way to do that is to create temp array for each
//copy the current arrays into temp arrays
//resize the current arrays
//put back the temp arrays in the current arrays
//in the newly sized arrays
//add the new product
//add the quantity
}
public void calcSubtotal(){
//TODO
//FOR LOOP THROUGH THE orderProduct array
//get the price
//get the quantity from the orderQuantity array
//STORE IT IN subtotal VARIABLE
double sum = 0;
for (int i = 0; i < orderProduct.length; i++)
{
sum += orderProduct[i].getPrice() * orderQuantity[i];
}
subtotal = sum;
}
public void calcTax(){
//TODO
//CALCULATE THE TAX
//STORE IT IN tax VARIABLE
tax = subtotal * TAX_RATE;
}
public void calcTotal(){
//TODO
//CALCULATE THE TOTAL
//STORE IT IN total VARIABLE
total = subtotal + tax;
}
public String toString(){
NumberFormat nf = NumberFormat.getCurrencyInstance();
String result = "";
result += "CASHIER @ REGISTER\n " + orderClerk.getFirstName() + " " + orderClerk.getLastName() +" @ " + orderClerk.getRegisterNbr() + "\n\n";
result += "CUSTOMER INFO\n " + orderCustomer.toString() + "\n\n";
result += "NUMBER OF ITEMS SOLD = " + orderProduct.length + "\n\n";
result += "TOTALS\n " + "Subtotal: " + nf.format(subtotal) + "\n"
+ "Tax (" + nf.format(TAX_RATE) + "): " + nf.format(tax) + "\n"
+ "Total : " + nf.format(this.total);
//TODO
//ADD REST OF SUMMARY TO RESULT
//SEE LINE ABOVE FOR EXAMPLE OF HOW TO GET INFORMATION FROM
//OTHER OBJECTS THAT ARE AVAILABLE WITHIN THE ORDER CLASS
//SEE PROJECT HANDOUT TO GET IDEA OF HOW YOUR SUMMARY SHOULD LOOK LIKE
return result;
}
//Returns the number of items sold
public int getNumberItemsSold(){
int totalQty = 0;
for (int i = 0;i<orderQuantity.length;i++){
totalQty += orderQuantity[i];
}
return totalQty;
}
}