Hello again,
This time I'm creating a program that creates an array of CD objects and then displays the information. I've created a CD class, a CDInventory class and then a CDInventoryDisp lay to use the previos two. I've gotten all of this code oto compile but I'm still doing something wrong. I'm about to pull my hair out! When I run my CDInventoryDisp lay program it comes back with this error:
C:\Documents and Settings\Sam>ja va CDInventoryDisp lay
Exception in thread "main" java.lang.NullP ointerException
at CDInventory.cal culateInventory Value(CDInvento ry.java:35)
at CDInventoryDisp lay.main(CDInve ntoryDisplay.ja va:11)
Here are my codes:
This time I'm creating a program that creates an array of CD objects and then displays the information. I've created a CD class, a CDInventory class and then a CDInventoryDisp lay to use the previos two. I've gotten all of this code oto compile but I'm still doing something wrong. I'm about to pull my hair out! When I run my CDInventoryDisp lay program it comes back with this error:
C:\Documents and Settings\Sam>ja va CDInventoryDisp lay
Exception in thread "main" java.lang.NullP ointerException
at CDInventory.cal culateInventory Value(CDInvento ry.java:35)
at CDInventoryDisp lay.main(CDInve ntoryDisplay.ja va:11)
Here are my codes:
Code:
// CD class // represents a compact disc public class CD { private String title; // CD title (name of product) private String number; // CD product number private int numStock; // CD stock number private double price; // price of CD // constructor initializes CD information public CD( String cdTitle, String productNumber, int numberInStock, double cdPrice ) { title = cdTitle; number = productNumber; numStock = numberInStock; price = cdPrice; } // end constructor public String toString() { return title + number + numStock + price; } // end method toString } // end class CD
Code:
// CDInventory.java // an inventory of CDs public class CDInventory { private CD compactDiscs[]; //declaration of an array of CD objects private double inventoryValue; private String titles[]; private String productNumbers[]; private int stockAmounts[]; private double prices[]; public CDInventory()//constructor fills CD inventory { String titles[] = { "Sixpence None the Richer" , "Clear" , "NewsBoys: Love Liberty Disco" , "Skillet: Hey You, I Love Your Soul" , "Michael Sweet: Real"}; String productNumbers[] = { "D121401" , "D126413" , "2438-51720-2" , "D122966" , "020831-1376-204" }; int stockAmounts[] = { 12, 15 , 7, 10, 9 }; double prices[] = {11.99 , 9.99 , 12.99 , 10.99 , 9.99}; compactDiscs = new CD[ 5 ]; // creation of an array of CD objects for (int count = 0; count < compactDiscs.length; count++ ) // populate inventory with CD objects { compactDiscs[ count ] = new CD( titles[ count ], productNumbers[ count ], stockAmounts[ count ], prices[ count ] ); }// end for } // end CDInventory constructor public void calculateInventoryValue( ) { double total = 0.00; for ( int count = 0; count < compactDiscs.length; count++ ) { total += stockAmounts[ count ] * prices[ count ]; } // end for inventoryValue = total; } //end calculateInventoryValue public double getInventoryValue() { return inventoryValue; } //end getInventoryValue public void toBuildString() { for( int count = 0; count < compactDiscs.length; count++ ) { System.out.printf( "%s%-34s%-16d%-11f%-22f\n", titles[ count ], productNumbers[ count ], stockAmounts[ count ], prices[ count ] ); }// end for } // end method toBuildString }// end class
Code:
// CDInventoryDisplay.java // inventory display application public class CDInventoryDisplay { // execute application public static void main( String args[] ) { CDInventory myCDInventory = new CDInventory(); myCDInventory.calculateInventoryValue(); System.out.printf( "%-34s%-16s%-16s%-15s", "Title", "Product Number" , "Amount in Stock" , "Item Price" ); System.out.printf( "%s%.2f" , "The total value of the inventory is: $", myCDInventory.getInventoryValue() ); } // end main } // end class
Comment