By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,052 Members | 1,235 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,052 IT Pros & Developers. It's quick & easy.

java.lang.NullPointerException error

sammyboy78
P: 21
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 CDInventoryDisplay 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 CDInventoryDisplay program it comes back with this error:

C:\Documents and Settings\Sam>java CDInventoryDisplay
Exception in thread "main" java.lang.NullPointerException
at CDInventory.calculateInventoryValue(CDInventory.ja va:35)
at CDInventoryDisplay.main(CDInventoryDisplay.java:11 )


Here are my codes:

Expand|Select|Wrap|Line Numbers
  1. // CD class
  2. // represents a compact disc
  3.  
  4. public class CD
  5. {
  6.     private String title; // CD title (name of product)
  7.     private String number; // CD product number
  8.     private int numStock; // CD stock number
  9.     private double price; // price of CD
  10.  
  11.     // constructor initializes CD information
  12.     public CD( String cdTitle, String productNumber, int numberInStock, double cdPrice )
  13.     {
  14.         title = cdTitle;
  15.         number = productNumber;
  16.         numStock = numberInStock;
  17.         price = cdPrice;
  18.  
  19.     } // end constructor
  20.  
  21.     public String toString()
  22.     {
  23.         return title + number + numStock + price;
  24.     } // end method toString
  25.  
  26. } // end class CD
Expand|Select|Wrap|Line Numbers
  1. // CDInventory.java
  2. // an inventory of CDs
  3.  
  4. public class CDInventory
  5. {
  6.     private CD compactDiscs[]; //declaration of an array of CD objects
  7.     private double inventoryValue;
  8.     private String titles[];
  9.     private String productNumbers[];
  10.     private int stockAmounts[];
  11.     private double prices[];
  12.  
  13.     public CDInventory()//constructor fills CD inventory
  14.     {
  15.         String titles[] = { "Sixpence None the Richer" , "Clear" , "NewsBoys: Love Liberty Disco" , "Skillet: Hey You, I Love Your Soul" , "Michael Sweet: Real"};
  16.         String productNumbers[] = { "D121401" , "D126413" , "2438-51720-2" , "D122966" , "020831-1376-204" };
  17.         int stockAmounts[] = { 12, 15 , 7, 10, 9 };
  18.         double prices[] = {11.99 , 9.99 , 12.99 , 10.99 , 9.99};
  19.  
  20.         compactDiscs = new CD[ 5 ]; // creation of an array of CD objects
  21.  
  22.         for (int count = 0; count < compactDiscs.length; count++ ) // populate inventory with CD objects
  23.         {
  24.             compactDiscs[ count ] = new CD( titles[ count ], productNumbers[ count ], stockAmounts[ count ], prices[ count ] );
  25.         }// end for
  26.  
  27.     } // end CDInventory constructor
  28.  
  29.     public void calculateInventoryValue( )
  30.     {
  31.         double total = 0.00;
  32.  
  33.         for ( int count = 0; count < compactDiscs.length; count++ )
  34.         {
  35.             total += stockAmounts[ count ] *  prices[ count ];
  36.  
  37.         } // end for
  38.  
  39.         inventoryValue = total;
  40.  
  41.     } //end calculateInventoryValue
  42.  
  43.     public double getInventoryValue()
  44.     {
  45.         return inventoryValue;
  46.  
  47.     } //end getInventoryValue
  48.  
  49.     public void toBuildString()
  50.     {
  51.         for( int count = 0; count < compactDiscs.length; count++ )
  52.         {
  53.             System.out.printf( "%s%-34s%-16d%-11f%-22f\n", titles[ count ], productNumbers[ count ], stockAmounts[ count ], prices[ count ] );
  54.         }// end for
  55.  
  56.  
  57.     } // end method toBuildString
  58.  
  59.  
  60. }// end class
Expand|Select|Wrap|Line Numbers
  1. // CDInventoryDisplay.java
  2. // inventory display application
  3.  
  4. public class CDInventoryDisplay
  5. {
  6.     // execute application
  7.     public static void main( String args[] )
  8.     {
  9.         CDInventory myCDInventory = new CDInventory();
  10.  
  11.         myCDInventory.calculateInventoryValue();
  12.  
  13.         System.out.printf( "%-34s%-16s%-16s%-15s", "Title", "Product Number" , "Amount in Stock" , "Item Price" );
  14.  
  15.         System.out.printf( "%s%.2f" , "The total value of the inventory is: $", myCDInventory.getInventoryValue() );
  16.  
  17.     } // end main
  18.  
  19. } // end class
Jun 16 '07 #1
Share this Question
Share on Google+
3 Replies


10K+
P: 13,264
A nullpointer exception is thrown when you try to dereference a variable pointing to null. In your method that is throwing this exception, you have three arrays that you are dereferencing. So one of them is null at the time of call. However, the jre has also given you the line number where the nullpointer was thrown. So if you look at your code at that line number, you can tell which array was null. This is how you should be removing errors/exceptions from your programs. The compiler or the jre always tries to give you the exact line number of the problem.
Jun 16 '07 #2

sammyboy78
P: 21
A nullpointer exception is thrown when you try to dereference a variable pointing to null. In your method that is throwing this exception, you have three arrays that you are dereferencing. So one of them is null at the time of call. However, the jre has also given you the line number where the nullpointer was thrown. So if you look at your code at that line number, you can tell which array was null. This is how you should be removing errors/exceptions from your programs. The compiler or the jre always tries to give you the exact line number of the problem.

Yeah, I had been working on line 35 for a couple of hours. I just don't know what to try anymore and I'm not sure what dereferencing is, I don't think I've gotten that far in my textbook yet. I don't understand why the arrays are null since they're getting initialized with values in the constructor. I'm only here because I can't find the answers in my textbook and I've already spent hours trying to fix the problems myself.

Be gentle, I'm only 5 weeks old...
Jun 16 '07 #3

Expert 10K+
P: 11,448
I don't understand why the arrays are null since they're getting initialized with values in the constructor.
No they're not: you defined a couple of variables local to the constructor again
and you initialized those instead of your member array variables. In other words:
your member array variables are still null which explains the exception that was
thrown at you.

kind regards,

Jos
Jun 16 '07 #4

Post your reply

Sign in to post your reply or Sign up for a free account.