Results 1 to 8 of 8
  1. #1
    Maricica is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Post [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.

    Java 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
    Last edited by Maricica; 03-29-2011 at 02:15 PM.

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Doubt many will read all that code posted without code tags or formatting. Regardless, you should probably be checking your length before accessing an index to guard against that exception.

  3. #3
    Maricica is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Thank you for your suggestion. I did that already. I check the list with isEmpty(), but it didn't help.

  4. #4
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Quote Originally Posted by Maricica View Post
    Thank you for your suggestion. I did that already. I check the list with isEmpty(), but it didn't help.
    Because two threads can find isEmpty returning true at similar times ...

  5. #5
    Maricica is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    But, if the variables from threads are local ones, is it important if different threads find isEmpty returning true to return that error?
    My threads do concurrent tasks in parallel, they don't work together to do a single job. They just have to generate and copy some files.

    I'm asking because threads are not my speciallity :).

  6. #6
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    If your threads both work on the List, (say for example it's a static List) then both can call the isEmpty method at similar times and get a true value. Then both can proceed to remove objects and it's a race as to who deletes before the List becomes empty. Better synchronize both the isEmpty check and the delete sections to make them one atomic operation.
    Last edited by r035198x; 03-29-2011 at 11:24 AM. Reason: Both =>Better

  7. #7
    Maricica is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    Besides the local lists of the threads which are cleared at the end of the thread, nothing is removed, only accesed or added.
    The static List is only filled inside the threads.

  8. #8
    Maricica is offline Member
    Join Date
    Mar 2011
    Posts
    5
    Rep Power
    0

    Default

    I've solved it. It was very simple, but I just couldn't see earlier. Sometimes the program was trying to use a local list that wasn't filled at all. Because of this it was thrown an exception when accessing the list, because the list was empty. Therefore the error was very logical.

Similar Threads

  1. Help Array Index out of bounds exception
    By star400040 in forum New To Java
    Replies: 2
    Last Post: 12-10-2010, 11:24 PM
  2. Array index Out of bound Exception
    By nitin_daviet88 in forum New To Java
    Replies: 9
    Last Post: 07-28-2010, 06:32 AM
  3. array Index out of Bounds exception== 0
    By Allgorythm in forum New To Java
    Replies: 6
    Last Post: 02-11-2010, 05:02 PM
  4. Array Index Out of Bounds Exception
    By kool001 in forum New To Java
    Replies: 1
    Last Post: 12-03-2009, 08:42 AM
  5. Array Index Out of bound exception
    By abhijit in forum Networking
    Replies: 7
    Last Post: 09-25-2009, 08:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •