Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 307,086 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 2,246 people online right now. Registration is fast and FREE... Join Now!




For Loop through an Array to Calculate a Subtotal

 

For Loop through an Array to Calculate a Subtotal

Splice

26 Apr, 2009 - 02:49 AM
Post #1

New D.I.C Head
*

Joined: 26 Apr, 2009
Posts: 2

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

}


User is offlineProfile CardPM
+Quote Post


janotte

RE: For Loop Through An Array To Calculate A Subtotal

26 Apr, 2009 - 03:30 AM
Post #2

code > sword
Group Icon

Joined: 28 Sep, 2006
Posts: 2,171



Thanked: 153 times
Expert In: C/C++

My Contributions
Welcome to DIC!

Are the arrays
orderProduct[]
and
orderQuantity[]
guaranteed to be the same length?

This post has been edited by janotte: 26 Apr, 2009 - 03:37 AM
User is offlineProfile CardPM
+Quote Post

Splice

RE: For Loop Through An Array To Calculate A Subtotal

26 Apr, 2009 - 12:49 PM
Post #3

New D.I.C Head
*

Joined: 26 Apr, 2009
Posts: 2

I thought they were, but assuming they weren't how would I solve this?
User is offlineProfile CardPM
+Quote Post

computerfox

RE: For Loop Through An Array To Calculate A Subtotal

26 Apr, 2009 - 01:08 PM
Post #4

straight vegetarian kid
*****

Joined: 29 Jan, 2009
Posts: 3,772



Thanked: 48 times
Dream Kudos: 1700
My Contributions
i don't think there would be a need to solve it. it's just that the two arrays must be the same size because it needs the whole information especially when calculating the whole cost. i was actually helping someone at school with a problem like this.
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/21/09 10:44AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month