• 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
  • Tim Cooke
  • paul wheaton
  • Paul Clapham
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Roland Mueller
  • Piet Souris
Bartenders:

Exception in Thread-Inventory Program

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can someone please help me understand why I'm getting an exception here?
_____________________________________________________________________________

run:
Welcome to inventory managment.

How many items?
3
Enter item name: Homo
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at inventory.Inventory.main(Inventory.java:39)
Java Result: 1
BUILD SUCCESSFUL (total time: 16 seconds)
_____________________________________________________________________________



Thanks in advance.
 
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I believe you should have got compilation error because what is "i" referred inside the loop?
 
Gus Moeller
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It was intended to be a variable for the array, initially line 37 used an i for the count variable, that's where I got the error. This code is the result of me trying to tweak it to get it to work. When I switched the count variable to a in line 37 I got an error in the array variable on line 40.
 
Arka Guhathakurta
Ranch Hand
Posts: 46
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,
I found 2 issues in your program
The first one:

I believe that You are initializing all the above array of zero length. count is zero and hence all the above arrays will have length zero. Now count does not count for array indices any more

You are taking input and storing it in count which overwrites the previous data.
The second one:

either keep "i" or keep "a". If you think a is for counter replace i with a or vice versa whichever suits you.
The ArrayIndexOutOfBoundsException is thrown because in line no 40 you are accessing something that does not actually exist. I mean anything like productName[0] or productname[1] does not exisst since you initialized the array to be of zero length.
 
Gus Moeller
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, thanks, I ended up getting it worked out, but those were exactly my problems.


This is what I ended up with, I know there's a lot of redundant loops in there, but it's due today. Hopefully I'll get time to clean it up later.

Thanks again.
reply
    Bookmark Topic Watch Topic
  • New Topic