tags:

views:

120

answers:

5

How can I fix this OutOfBoundsException?

Here is the code I am using:

 ResultSet rsTagCheck = stmt.executeQuery(
     "SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS, PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'");
 while (rsTagCheck.next()){
     String TagNum = rsTagCheck.getString("XKRPRMT_TAG");
     ArrayList<String> myTag = new ArrayList<String>();

     for (int i = 0; i < TagNum.length(); i++){
         myTag.add(TagNum);
         myTag.get(i + i);

I kinda know why I am getting the error, but I am not sure how to remedy the problem.

+2  A: 
Pointy
It's i+i not i+1.
Erick Robertson
With the sql statement I should get more than one tagnum from each user, I am trying to add all the tagnum's to an arraylist and display them in a dropdown box later in the code. I am kinda new to this so I am not entirelly sure how to preform this
gary A.K.A. G4
@Pointy I've undeleted my answer. Thanks :-)
Giu
@gary, the thing is, "TagNum" is just a String - when you call ".length()" you're getting the length of that one string in characters.
Pointy
yeah, I changed that to myTag.size()
gary A.K.A. G4
+10  A: 

The problem is the i+i part in myTag.get(i+i). It'll work for i=0, but as soon as i=1, you'll get an exception thrown, since you've added two elements to myTag, but are accessing the third element (myTag.get(2)).

Giu
I don't think it'll work when "i" is zero. How can it? The call to `get` will be trying to get element 1, and there will be no element 1 at that point. The exception he reports can't have come from that code as posted here.
Pointy
It's i+i not i+1.
Erick Robertson
@Péter Török: As Pointy already mentioned in his now updated answer, it'll *correctly* crash on the second iteration, exactly when `i=1`. With `i=0` you'll get the very first element, since in the line before `myTag.get(0)` an element is inserted; `myTag.get(0)` will return that element. The thing crashes when `i=1`, because exactly two elements have been inserted then, but it's trying to get the third element by invoking `myTag.get(2)`
Giu
@Giu, yes, may bad - when I noticed that I misread `i+i`, I couldn't fix my comment because your answer was in deleted state. Please accept my apologies and an upvote as compensation :-)
Péter Török
@Péter Török: Apologies accepted; I'm sorry for trying to delete my answer :-)
Giu
+1  A: 

This myTag.get(i + i); is causing the exception.

First time in the loop i is 0, you add an item into the ArrayList and then call get(0+0) which is fine.

In the next iteration, you add another element(total of 2 element in the list now) and call get(1+1), this causes exception as you have only 2 elements and valid index are 0 and 1.

codaddict
+2  A: 

You are using the for loop by iterating on the String TagNum. You should only need to say: myTag.add(TagNum).

Imagine that the String TagNum has 4 characters. You add the String to the list 4 times, but when you reach i = 3, you are trying to retrieve the element at position 3 + 1, but the list has elements from 0 to 3.

Also, try replacing the BannerID with a ? and set the parameter to the statement accordingly.

virgium03
He'd have to turn it into a PreparedStatement instead of just a Statement. (And yes that'd be a good idea.)
Pointy
+1  A: 

Even without the problem with the get, your program as written will read through the results of the query, and then for each CHARACTER in tagNum, it will add an instance of tagNum to your array. So if tagNum is, say, "ABC", the array will end up containing "ABC" three times. If tagNum is "ABCD", it will contain "ABCD" four times. This doesn't make a lot of sense.

I think what you want is to just add tagNum to an array, defining the array OUTSIDE of the ResultSet.next loop. Something like this maybe:

 ArrayList<String> myTag = new ArrayList<String>(); 
ResultSet rsTagCheck = stmt.executeQuery( 
  "SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS,   PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'"); 
while (rsTagCheck.next()){ 
  String TagNum = rsTagCheck.getString("XKRPRMT_TAG"); 
  myTag.add(TagNum); 
}

(Of course this doesn't use any of the other data in your query and I don't know what all else you're up to, but I believe that's what you're trying to do for this part.)

Update

Suppose you have ten records in your database table. After the above loop is complete, the array should be populated.

Try something like this:

ArrayList<String> myTag = new ArrayList<String>(); 
ResultSet rsTagCheck = stmt.executeQuery( 
  "SELECT PARKING.XKRPRMT.XKRPRMT_PIDM, PARKING.XKRPRMT.XKRPRMT_STATUS,   PARKING.XKRPRMT.XKRPRMT_EXPIRE_YR, PARKING.XKRPRMT.XKRPRMT_TAG FROM PARKING.XKRPRMT WHERE XKRPRMT_PIDM ='" + BannerID + "'"); 
while (rsTagCheck.next()){ 
  String TagNum = rsTagCheck.getString("XKRPRMT_TAG"); 
  myTag.add(TagNum); 
}
for (String tag : myTag)
{
  System.out.println(tag);
}

That should give you the list of all the tags. Note you have to examine the List AFTER the while(ResultSet) loop ends. Inside the loop you will only have the elements read so far.

If you're still getting only one value, make sure that you have more than one record coming back from the result set. Like, run the query outside of a Java program and see how many records you get.

Jay
@Jay, thanks I did that and it resolved some of the issues. What I want is sense a PIDM can have multiple TAG's I want to display all the TAG's in a drop down box. So I thought about populating an array and then display all elements in the array, but I get the the first element (myTag.get(0)) but then I get an outofbounds exception when I try to display anything passed the first element.
gary A.K.A. G4
@G4: See update.
Jay