| Author |
Exception in Thread-Inventory Program
|
Gus Moeller
Greenhorn
Joined: Feb 25, 2009
Posts: 3
|
|
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.
|
 |
Arka Guhathakurta
Ranch Hand
Joined: Mar 01, 2009
Posts: 46
|
|
Hi,
I believe you should have got compilation error because what is "i" referred inside the loop?
|
regards,
Arka
|
 |
Gus Moeller
Greenhorn
Joined: Feb 25, 2009
Posts: 3
|
|
|
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
Joined: Mar 01, 2009
Posts: 46
|
|
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
Joined: Feb 25, 2009
Posts: 3
|
|
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.
|
 |
 |
|
|
subject: Exception in Thread-Inventory Program
|
|
|