• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays and Methods  RSS feed

 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm having some trouble developing this program. We are supposed to enter the type of cookie, the number of boxes, and the cost per box. We then print each cookie, it's price, the number we order and the total cost. Then we print the total number of boxes for all cookies and the total cost of all cookie. We must accumulate these totals as stated in line 12 of the driver and have methods to calculate these totals. I am unsure of how to write these methods. I know that for calcBoxes, I must get the number of cookies for a certain type and add that to the total. For calcCookieCost I must get the number of boxes from caclBoxes and multiply this by the price per box. For right now, I'm multiplying by 1 to avoid red Xs while I figure this out. When I run this program, I get an exception, but I don't know why I am receiving one.







 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The exception is telling you you are trying to access an object but the variable that holds the object reference is null ie it isn't referencing an object. It also tells you the problem is in the CookieDriver class on line 74. So if you look at that line you can see the following:

Now numBoxes is a primitive type so can't be null therefore the problem must either be that cookies is null or the element at index i in the cookies array is null.

The easiest way to check is to put a print statement before line 73 to output cookies. If it prints 'null' you know the problem is that cookies is null if it prints anything else you know the element at index i is null. You then need to look at your code and work out why this is happening.
 
Sam Pauken
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've moved my print statements around. I'm not 100% sure, but it looks like the array index at i is null, although I don't understand why this is the case. I've know all along that lines 74 and 85 are problematic, but I don't know how to add the inputted number in these methods.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, so if an element in the array is null you need to make sure you are filling every element of the array with a Cookies object or you need to test for null in your loop.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sam Pauken wrote:I'm having some trouble developing this program.

Sam,

Please DontWriteLongLines. It makes your thread very hard to read, and it's actually bad coding practice.
I've broken yours up this time, but for future reference, please remember:
80 characters max.
(the SSCCE page actually recommends 62)
And that includes string literals AND comments AND long method calls.

Thanks.

Winston
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Notice that you loop over the cookies array in both the printArray method and the calcBoxes method but you don't get a NullPointerException in the printArray method. So take a look at how your for loops differ in those two methods.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!