java.lang.NullPointerException error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sammyboy78
    New Member
    • Jun 2007
    • 21

    java.lang.NullPointerException error

    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:

    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
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    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.

    Comment

    • sammyboy78
      New Member
      • Jun 2007
      • 21

      #3
      Originally posted by r035198x
      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...

      Comment

      • JosAH
        Recognized Expert MVP
        • Mar 2007
        • 11453

        #4
        Originally posted by sammyboy78
        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

        Comment

        Working...