[SOLVED] Java IndexOutOfBounds Exception: Index 0, Size 0
Hello,
I'm confronting with a strange problem. I'm writing some code to deliver data for a website. Because in my program I put the condition Thread.sleep(2000), to make shorter the time for delivery of data I created another program, with the same code as the first program, excepting the instruction Thread.sleep(2200).
In both programs I'm working with threads. If only one program works to process the data, everything is fine. The program doesn't have any problem. But when both programs work, from time to time it throws exception IndexOutOfBounds Exception: Index 0, Size 0.
The code that throws the exception is listed bellow. I mention that the iderr list is declared private static inside of the class which contains the StringTask class.
Code:
private final static class StringTask implements Callable<String> {
final String usertxt;
final List<String> cod_client_user = Collections.synchronizedList(new ArrayList<String>(500));
final List<Integer> idlist = Collections.synchronizedList(new ArrayList<Integer>(500));
public StringTask(String usertxt, List<String> cod_client_user, List<Integer> idlist) {
this.usertxt = usertxt;
this.cod_client_user.addAll(cod_client_user);
this.idlist.addAll(idlist);
}
public String call() {
if ((!cod_client_user.isEmpty())&&(!idlist.isEmpty())) {
List<Integer> idcopy = Collections.synchronizedList(new ArrayList<Integer>());
List<Integer> iderrcopy = Collections.synchronizedList(new ArrayList<Integer>());
List<String> codclerr = Collections.synchronizedList(new ArrayList<String>());
idcopy.clear();
iderrcopy.clear();
codclerr.clear();
String text = null;
String ok = "Success";
int result = -1;
boolean novalidpartner = true;
int nr =0;
JCoFunction functiondoi = null;
JCoTable tracks = null;
nr = this.cod_client_user.size();
{
try {
functiondoi = ABAP_AS.getRepository().getFunction("ZFMCS_TM_GETCLI");
if (functiondoi == null) {
for (Integer i1: this.idlist)
iderr.add(i1);
sendLogMailTo("Functia de generare a fisierelor .txt in SAP, ZFMCS_TM_GETCLI, nu a fost gasita in Repository - user: "+this.usertxt);
return "Error";
}
try {
JCoContext.begin(ABAP_AS);
tracks = functiondoi.getTableParameterList().getTable("IM_GPART");
tracks.appendRows(nr);
for (int i =0; i<nr; i++) {
tracks.setValue("SUSER",this.usertxt);
tracks.setValue("GPART", this.cod_client_user.get(i));
tracks.nextRow();
}
}
catch(Exception e){
for (Integer i1: this.idlist)
iderr.add(i1);
sendLogMailTo("Tabelul IM_GPART cu datele de parametri de import pentru user-ul "
+this.usertxt+" nu s-a populat corect in SAP. Eroare: "+e.toString());
return "Error";
}
}
catch(Exception e) {System.out.println(e);}
System.out.println ("SAP START:"+ new Date());
try {
functiondoi.execute(ABAP_AS);
}
catch(Exception e){
if (((e.toString()).equals("com.sap.conn.jco.AbapException: (126) NO_VALID_PARTNER: NO_VALID_PARTNER"))) {
System.out.println("NO VALID PARTNER. Eroare acceptata.");
sendLogMailTo("Functia SAP ZFMCS_TM_GETCLI a ridicat o exceptie: "+e.toString()+". Nici un cod de client SAP valid pentru userul "
+this.usertxt+". ");
for (Integer i1: this.idlist)
iderr.add(i1);
return "Error";
}// END IF
else {
System.out.println("Eroare neacceptata");
for (Integer i1: this.idlist)
iderr.add(i1);
novalidpartner = false;
sendLogMailTo("Functia SAP ZFMCS_TM_GETCLI nu s-a executat corect. Eroare pentru userul "+this.usertxt+": "+e.toString());
return "Error";
} //END ELSE
}//END CATCH
int err = 0;
String txt = null;
String isvalid=null;
try{
System.out.println("err="+err);
for (int i =0; i<nr; i++) {
tracks.setRow(i);
isvalid=tracks.getString("ISVALID");
if (isvalid.equals("X"))
idcopy.add(this.idlist.get(i));
else {
err = -1;
iderrcopy.add(this.idlist.get(i));
codclerr.add(this.cod_client_user.get(i));
}//END ELSE
}// END FOR
JCoContext.end(ABAP_AS);
}//END TRY
catch(Exception e) {
for (Integer i1: this.idlist)
iderr.add(i1);
sendLogMailTo("Probleme la datele de iesire din SAP. Nu se stie daca fisierele .txt au fost generate corespunzator pentru userul "
+this.usertxt+". Eroare: "+e.toString());
return "Error";
}//END CATCH
tracks.deleteAllRows();
System.out.println ("Sap END:"+ new Date());
if (err ==0) {
result = 0;
System.out.println ("Copy1 START:"+ new Date());
result = CmdExec(this.usertxt); //COPIES SOME FILES, doesn't have lists at all
System.out.println ("Copy1 END:"+ new Date());
}
else {
txt = "Nu s-au generat corect fisierele .txt in SAP pentru userul "+this.usertxt+". Codurile de client cu probleme cu id-urile din tabela: ";
for (int k=0;k<iderrcopy.size();k++)
txt = txt+iderrcopy.get(k)+") "+codclerr.get(k)+"; ";
sendLogMailTo(txt);
if (!(idcopy.isEmpty())) {
System.out.println ("Copy1 START:"+ new Date());
result = CmdExec(this.usertxt);
System.out.println ("Copy1 END:"+ new Date());
ok = "Error";
}//END IF
else {
sendLogMailTo("Nici un cod client SAP valid pentru userul "+this.usertxt+".");
return "Error";
} //END ELSE
}//END ELSE
String comanda = null;
int count = 0;
Statement s = null;
try {
s = conn.createStatement ();
}
catch(Exception e) {
sendLogMailTo("Update-ul pentru userul "+this.usertxt+
" nu s-a realizat, probleme cu conexiunea la baza de date. Eroare: "+e.toString());
return "Error";
}
if (result ==0) {
try {
if (!idcopy.isEmpty()) {
System.out.println ("Update1 START:"+ new Date());
if (idcopy.size()>=2) {
comanda = "update sitebox.members_requests set sap_status = 1, server=1, request_delivered = '"
+new java.sql.Timestamp(new java.util.Date().getTime())+"' where id in (";
for (int i=0; i<=idcopy.size()-2; i++) {
comanda = comanda +(idcopy.get(i)).toString()+",";
}// END for
comanda = comanda +(idcopy.get(idcopy.size()-1)).toString()+")";
count = s.executeUpdate(comanda);
}//END if
else
if (idcopy.size() == 1) {
comanda = "update sitebox.members_requests set sap_status = 1, server=1, request_delivered = '"
+new java.sql.Timestamp(new java.util.Date().getTime())+"' where id ="+(idcopy.get(0)).toString();
count = s.executeUpdate(comanda);
}
System.out.println ("Update2)1 END:"+ new Date()+" count ="+count);
}//END if
}//END try
catch(Exception e) {
sendLogMailTo("Update-ul cu 1 (cerere rezolvata)in tabela members_requests s-a realizat cu erori pentru userul "
+this.usertxt+" prin comanda "+comanda+". "+e.toString());
}
try {
if (!iderrcopy.isEmpty()) {
System.out.println ("Update12 START:"+ new Date());
if (iderrcopy.size()>=2) {
comanda = "update sitebox.members_requests set sap_status = 2 , server=1, request_delivered = '"
+new java.sql.Timestamp(new java.util.Date().getTime())+"' where sap_status!=1 and id in (";
for (int i=0; i<=iderrcopy.size()-2; i++) {
comanda = comanda +(iderrcopy.get(i)).toString()+",";
}
comanda = comanda +(iderrcopy.get(iderrcopy.size()-1)).toString()+")";
count = s.executeUpdate(comanda);
}//END if
else
if (iderrcopy.size() == 1){
comanda = "update sitebox.members_requests set sap_status = 2 , server=1, request_delivered = '"
+new java.sql.Timestamp(new java.util.Date().getTime())+"' where sap_status!=1 and id ="
+(iderrcopy.get(0)).toString();
count = s.executeUpdate(comanda);
System.out.println("comanda = "+comanda);
}
System.out.println ("Update12 END:"+ new Date()+" count ="+count);
}//END if
} //END try
catch(Exception e) {
sendLogMailTo("Update-ul cu 2(cerere cu erori)in tabela members_requests s-a realizat cu erori pentru userul "
+this.usertxt+" prin comanda "+comanda+". "+e.toString());
}
System.out.println("Success!!!!!!!!");
idcopy.clear();
iderrcopy.clear();
codclerr.clear();
try{
s.close ();
}
catch(Exception e) {
sendLogMailTo("Update-ul pentru userul "+this.usertxt+" s-a realizat cu erori de inchidere de Statement. Eroare: "+e.toString());
return "Error";
}
return ok;
}
else {
for (Integer i1: idcopy) iderrcopy.add(i1);
txt = "Nu s-au copiat corect fisierele .txt in SAP pentru userul "+this.usertxt+". Codurile de client cu probleme cu id-urile din tabela: ";
for (int k=0;k<iderrcopy.size();k++)
txt = txt+iderrcopy.get(k)+") "+codclerr.get(k)+"; ";
try {
if (!iderrcopy.isEmpty()) {
System.out.println ("Update12 START:"+ new Date());
if (iderrcopy.size()>=2) {
comanda = "update sitebox.members_requests set sap_status = 2, server=1, request_delivered = '"
+new java.sql.Timestamp(new java.util.Date().getTime())+"' where sap_status!=1 and id in (";
for (int i=0; i<=iderrcopy.size()-2; i++) {
comanda = comanda +(iderrcopy.get(i)).toString()+",";
}
comanda = comanda +(iderrcopy.get(iderrcopy.size()-1)).toString()+")";
count = s.executeUpdate(comanda);
}
else
if (iderrcopy.size() == 1) {
comanda = "update sitebox.members_requests set sap_status = 2, server = 1, request_delivered = '"
+new java.sql.Timestamp(new java.util.Date().getTime())+
"' where sap_status!=1 and id ="+(iderrcopy.get(0)).toString();
count = s.executeUpdate(comanda); System.out.println("comanda = "+comanda);
}
System.out.println ("Update12 END:"+ new Date()+" count ="+count);
}
}
catch(Exception e) {
sendLogMailTo("Update-ul cu 2(cerere cu erori)in tabela members_requests s-a realizat cu erori pentru userul "
+this.usertxt+" prin comanda "+comanda+". "+e.toString());
}
idcopy.clear();
iderrcopy.clear();
codclerr.clear();
sendLogMailTo(txt);
try{
s.close ();
}
catch(Exception e){}
return "Error";
}
}
}
return "Succes";
}
}
Thanks in advance.
Maricica