ArrayIndexOutOfBoundsException: 1 >= 1 during a "while" statement...

hi,i'm getting an ArrayIndexOutOfBoundsException: 1 >= 1 in this method.

what this method does is receive an IP address from a multicast datagram packet,checks if the address is in the Vector elem,if not he puts it in,and if it's already there he substitutes the old infos with the new ones and wraps everything in a "Remote" object

something must be wrong in the while segment,but i can't find out....

boolean found = false

int c= 0;

synchronized(elem){

if(elem.size()>0){

while(c < elem.size() || found != true){

if(elem.elementAt(c).get_ind().equals(ip)){

elem.insertElementAt(new Remote(ip,data,num_svolti,media),c);

elem.removeElementAt(c+1);

found = true;

}

else c++;

}//END WHILE

if (found == false){

elem.add(new Remote(ip,data,num_svolti,media));

System.out.println("aggiunto nuovo remoto");

}

}

else {elem.add(new Remote(ip,data,num_svolti,media));System.out.println("new remote added");}

}

}

[1055 byte] By [Nihcolasa] at [2007-11-27 9:14:53]
# 1

> while(c < elem.size() || found != true){

Think what happens when found is still false, and you have eventually increased c such that it is >= elem.size().

You're still in the loop, so the next line:

> if(elem.elementAt(c).get_ind().equals(ip)){

goes ka-boom.

So obviously the condition to exit the loop is incorrect.

You probably want:

while ((c < elem.size()) && !found)

warnerjaa at 2007-7-12 22:03:28 > top of Java-index,Java Essentials,Java Programming...
# 2

while(c < elem.size() || found != true){

if(elem.elementAt(c).get_ind().equals(ip)){

elem.insertElementAt(new Remote(ip,data,num_svolti,media),c);

elem.removeElementAt(c+1);

found = true;

} else c++;

}//END WHILE

This looks to be your problem.

First, you have an OR in the while - if the element is not found the while will keep on going.

Second, you have "elem.removeElementAt(c+1)" - if size is 1 and c is 0, you are looking at element 1 which does not exists.

jbisha at 2007-7-12 22:03:28 > top of Java-index,Java Essentials,Java Programming...
# 3
Note to self: Add "Nihcolas" to the list of ingrates.
warnerjaa at 2007-7-12 22:03:28 > top of Java-index,Java Essentials,Java Programming...