• Post Reply Bookmark Topic Watch Topic
  • New Topic

add() method of ArrayList giving IndexOutOfBoundException  RSS feed

 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I hope this is the correct forum to post this doubt. I'm getting stuck inside a model class in my Struts 1 web application, but the error appears plain Java related, so I'm posting here.

Ok...so I have a method as given below :



When this code is getting executed, I am getting the exception :



Following the different print statements, I can see that the size of the arraylist userList is 0, and hence userList.add() is throwing this exception.

Can anyone please point out what is it that I am doing wrong?
Thanks.....
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


The problem is in the execute() method of your action class AllUserRetrieveAction. Check it carefully. You are trying to access some array of size 1 with an index which is also 1. That is the flaw. You will have to check your logic.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your code, the exception stack trace, and what you're describing don't jive.

If you look at the docs for List.add(int, E), you'll see that you can only add in the range 0..size(). So if you have 2 elements in the list (which are are indices 0 and 1), you can only add at indices 0, 1, or 2. This makes sense, because with your new element in it, the list will only have 3 positions filled, so you have to pick one of them to place your element at.

If you're still having trouble, please provide an SSCCE(←click) so that we can see your actual code and the actual error message.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is another aspect that I feel people neglect Jeff. Attempts to learn a framework without having the language basics clear is unwise in my opinion. Apache-OpenSymphony struts is a framework to make web application development easier / faster, but you still ought to know your Java well. Without having the fundamentals of the langauge clear, learning the framework will only make things more confusing. What are your thoughts on this?
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Mansukhdeep...the only place I am trying to access the ArrayList is when I am trying to add an entry. The very first time I'm trying to do it there's the exception. As the code shows, the ArrayList ought to be empty before the first time this add is attempted.

Hi Jeff...I'm not sure what other information I can give to make things clearer without introducing unnecessary details. But I'll try.

The quantinst.user table has a list of all users in the system (currently 7 in all). The different print statements give the following output :



The Action class from where this is being called is as follows :

The first print statement here is also getting printed as expected. But the last print statement, which ought to print the entire list, is not getting printed. this looks okay to me, as the exception is being thrown before this, and the execution never reaches this line.

Two other things.
1. While running the application, when I am encountering the error, I am getting an HTTP Status 500 error on the browser.
2. I am running this application from inside the Netbeans 7.0 IDE and the associated container is Glassfish Server 3.1

If there is any other information you think I missed out, please let me know.
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, Mansukhdeep.....I see the API for ArrayList.add method that I'm using is


I have made the ArrayList for only strings, and an arraylist need not be initialized with a specific size before using it, so I've not given it a size.

If the error is somewhere in the Action class's execute() method, then why do the


statements print all the users retrieved by the resultset? If issue is in action class, then atleast this code block should have executed completely and correctly, right?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So the problem is get(), not add() as your subject line and initial post say.

The error message is telling you exactly what's wrong:


At line 43 of AllUserRetrieveAction.java, you call ArrayList.get(). You are trying to get the 2nd element (index 1) of an array that has only 1 element (index 0).



That's wrong for a couple reasons.

1. You shouldn't be iterating with get(). you should use a foreach loop or an explicit Iterator.

2. Indices are 0..size() - 1, not 1..size(). So if you were going to iterate with get() (but don't do that), it would be for (int i = 0; i < numberOfUsers, i++). But don't do that.

 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Jeff,
Thanks. That worked. The problem is...the if(rs.next()) code block in the retrieveAllUsers() method is finding only 1 result. There should ideally be 7 values in the resultset, not 1. So after retrieving that, it is returning the result back to the action class, which is then trying the get(). This is what made me think that there is some error with the add() function.
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If your business logic requires that there be 7 objects, then you should throw an exception if that condition is not met.
 
Souvvik Basu
Ranch Hand
Posts: 96
Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No...my business logic doesn't require that there be 7 objects. I meant...there are currently 7 users there. It should be dynamic, increasing or reducing with time as users are added to/removed from the system.
 
Mansukhdeep Thind
Ranch Hand
Posts: 1163
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Souvvik Basu wrote:Hi Jeff,
Thanks. That worked. The problem is...the if(rs.next()) code block in the retrieveAllUsers() method is finding only 1 result. There should ideally be 7 values in the resultset, not 1. So after retrieving that, it is returning the result back to the action class, which is then trying the get(). This is what made me think that there is some error with the add() function.


If this is the case, I think you should be using while(rs.next) and then perform the business logic inside it instead of if(rs.next()). If construct shall only check if a single record is present not 7 records.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!