473,395 Members | 1,452 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

java.lang.NullPointerException error

sammyboy78
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
3 3566
r035198x
13,262 8TB
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
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
JosAH
11,448 Expert 8TB
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

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

Similar topics

1
by: Jens Mueller | last post by:
Hi there, this is a Java-XML Question, so I am not sure whether this is the right place, haven't found anything better .... I try to convert a Java object to XML via SAX and let the FOP...
2
by: Tim Murray | last post by:
First of all, I don't know much about Java, even its naming and version numbering nomenclature, and second, if there is a better group to ask this in, please let me know. System is Mac with...
12
nomad
by: nomad | last post by:
Hi everyone; My Class has ended and I was not able to solve this problem in time, and I would still like to solve it. I got these error code. Exception in thread "main"...
0
by: gezkk | last post by:
hi, i m using http proxy service to get proxy and host to applet, its working fine with jdk1.5 but getting java.lang.NullPointerException in jdk1.6 And the following is mseeage contains java...
0
by: ycinar | last post by:
hey all, i am working on a Java project using JBuilder.. when i build my code its fine, but when comes to run, it doesnt run and displays the following logs.. i think there is JDK conflict.. ...
1
by: ketand1 | last post by:
import java.awt.*; import java.awt.event.*; import java.io.*; import java.util.*; import java.sql.*; import java.lang.*; class DbAwt extends Frame implements ActionListener { private...
2
by: chokcheese | last post by:
I'm having trouble with a java application. When I try and run the program it shows a java.lang.NullPointerException in the "tic.getClient().getClientTicketList().add(tic);" line (it's in bold). I...
2
by: lilyumestar | last post by:
This project is due by Tuesday and I haven't even gotten half of it done. Can anyone please help me with this Exception error? I've been trying to figure it out for several hours Error Message ...
3
by: ohadr | last post by:
hi, i get Exception in thread "main" java.lang.NullPointerException when i run my application. the exact error is: "Exception in thread "main" java.lang.NullPointerException at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.