File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes arraylist - error Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arraylist - error" Watch "arraylist - error" New topic
Author

arraylist - error

kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
here are some of my codes.
i try to add an item to my arraylist from a GUI class.

i cannot add a second item after i have added one item.



i get : java.lang.IndexOutOfBoundsException: Index: 1, Size: 1
arraylist should adjust the size automatically?
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
The error message likely also suggests a line of code where the error originates. Which line of code is it?


[How To Ask Good Questions] [JavaRanch FAQ Wiki] [JavaRanch Radio]
Dirk Schreckmann
Sheriff

Joined: Dec 10, 2001
Posts: 7023
Also, I don't see a main method in the posted code. So, apparently there's more to this application that could possibly be the source of the problem.

Are you sure the problem originates from the posted code and not from something else?
Blake Minghelli
Ranch Hand

Joined: Sep 13, 2002
Posts: 331
Yes, a List will automatically "grow" when new objects are added. However, you can still get an IndexOutOfBoundsException if you try to do a list.get(i) and i is a value greater then or equal to list.size(). So, if your list has one object in it and you did list.get(1), you'd get an IndexOutOfBoundsException because list.get(1) assumes there are at least 2 objects in the list.

It's hard to tell exactly what your code is doing since it's in a different language, but this part of the code looks like the culprit:

I believe the second conditional should be rewritten like this:

Try that out, and let us know if you're still having trouble.


Blake Minghelli<br />SCWCD<br /> <br />"I'd put a quote here but I'm a non-conformist"
kelvin cheung
Ranch Hand

Joined: Mar 27, 2004
Posts: 120
hi Blake,
you were right! you saw the problem ^^ ...

but it was me that forgot a silly thing.
i should use on both:

list.size()>i

thats why i defined a variable (int i = 0).

and i found out that to use "while" on the first, will totally give me no problems ^^-..



thanks for the replies
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: arraylist - error