This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
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 Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "arraylist - error" Watch "arraylist - error" New topic

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

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

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:


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:
subject: arraylist - error
Similar Threads
dealing with compiler warnings
I don't understand why my JTable will not display data
why the program keep looping
Problem Interface/Abstract/Collections