Results 1 to 3 of 3
  1. #1
    Preethi is offline Member
    Join Date
    Jan 2008
    Posts
    83
    Rep Power
    0

    Default ArrayIndexout of Bound exception

    java.lang.IndexOutOfBoundsException: Index: 21, Size: 21
    at java.util.ArrayList.RangeCheck(ArrayList.java:546)
    at java.util.ArrayList.get(ArrayList.java:321)
    at saxExamples.DataReader.list(DataReader.java:94)
    at saxExamples.DataReader.main(DataReader.java:110)
    while excecuting my program i'm getting this exception...How to avoid this and make my program run?

    This is my program:
    Java Code:
    package saxExamples;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.text.ParseException;
    import java.text.SimpleDateFormat;
    import java.util.ArrayList;
    import java.util.Collections;
    import java.util.Date;
    import java.util.Iterator;
    import java.util.List;
    import org.xml.sax.SAXException;
    
    public class DataReader 
    {
        protected ArrayList<DataObject> res_mseg;
        protected ArrayList<DrawingData> rev_list;
        protected ArrayList<FromDateData> frm_list;
        protected ArrayList<WrkorderData> wrk ;
           
        public DataReader(){
            res_mseg = new ArrayList<DataObject>();
            rev_list = new ArrayList<DrawingData>();
                   
        }
        
        public void getData()throws Exception
        {
        
            XMLReading xmlreadingObj = new XMLReading();
            xmlreadingObj.fileParsing("C:/Documents and Settings/Gantt.xml");
            xmlreadingObj.print();
            xmlreadingObj.printDrawingData();
            xmlreadingObj.printFromdate();
            xmlreadingObj.printToDate();
            xmlreadingObj.printToTime();
            xmlreadingObj.printFrmtime();
            
            
        }
        public void list() throws SAXException, FileNotFoundException, IOException, ParseException
        {
          
            XMLReading xmlreadingObj = new XMLReading();
            xmlreadingObj.fileParsing("C:/Documents and Settings/Gantt.xml");
            
            res_mseg = xmlreadingObj.res_list;
            Object [] array = res_mseg.toArray();
            
            rev_list = xmlreadingObj.rev__wrkOrder_list;
            Object [] array1 = rev_list.toArray();
            
            wrk = new ArrayList<WrkorderData>();
            wrk = xmlreadingObj.wrkorder_list;
            
            frm_list = xmlreadingObj.rev_frmdate_list;
                                          
            String str1 = new String();
            String str2 = new String();
            ArrayList<String> list = new ArrayList();
            ArrayList list1 = new ArrayList();
            ArrayList<Date> l = new ArrayList<Date>();
           
            for(int len = 0; len < array.length; len++)
            {
               str1 = array[len].toString();
               for(int len1 = 0; len1 < array1.length; len1++)
               {
                   str2 = array1[len1].toString();
                   if(str1.compareToIgnoreCase(str2) == 0 )
                   {     
                       list.add(frm_list.get(len1).toString());
                       list1.add(wrk.get(len1));
                   }
                
                }
               
               for(int wrk_len= 0;wrk_len<wrk.size();wrk_len++)
                       {
                            String str = list.get(wrk_len).toString();
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = df.parse(str);
                           
                            l.add(date);
                                                    
                       }
                System.out.println(l.get(0).getTime()-l.get(1).getTime());
                list.clear();
              }
        }
           
    public static void main(String args[])
        {
            DataReader Obj = new DataReader();
           
            try 
            {
               Obj.list();
                //Obj.getData(); 
            }
            catch (Exception ex) 
            {
                ex.printStackTrace();
            }
        } 
    
        
       
    }

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Error is on one of your arrays, may be adding more data then specified size of it. Better to check it. I'll check it after going home. In this machine I don't have install in JDK :)

  3. #3
    jelly's Avatar
    jelly is offline Member
    Join Date
    Jan 2008
    Location
    Somerset, UK
    Posts
    46
    Rep Power
    0

    Default

    I suspect your problem is in this piece of code
    Java Code:
               for(int wrk_len= 0;wrk_len<wrk.size();wrk_len++)
                       {
                            String str = list.get(wrk_len).toString();
                            SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
                            Date date = df.parse(str);
                           
                            l.add(date);
                                                    
                       }
    your for loop is limited by the ArrayList wrk:

    wrk_len<wrk.size()

    but the code inside the loop works on the ArrayList list:

    String str = list.get(wrk_len).toString();

    I think either the test in the loop is wrong or the ArrayList you are operating on is wrong
    -- Hope that helps

Similar Threads

  1. Need help on Exception
    By Deon in forum New To Java
    Replies: 7
    Last Post: 02-11-2010, 05:46 PM
  2. Where does the exception go?
    By aytidaalkuhs in forum New To Java
    Replies: 3
    Last Post: 04-07-2008, 02:24 PM
  3. exception
    By Oktam in forum New To Java
    Replies: 2
    Last Post: 03-23-2008, 07:01 PM
  4. Replies: 1
    Last Post: 03-08-2008, 06:41 PM
  5. Exception
    By Camden in forum New To Java
    Replies: 2
    Last Post: 11-26-2007, 11:50 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
  •