• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Arrays and Methods

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.







 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 679
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic