Results 1 to 13 of 13
  1. #1
    JetLagFox is offline Member
    Join Date
    Jan 2017
    Posts
    15
    Rep Power
    0

    Question Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    I know that is an error that has already been asked many times, but I can't find where I have the problem. The error It is shown is the following:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 393, Size: 393
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at scraping.complementos_juegos.main(complementos_jue gos.java:305)
    There are many things I don't understand. The first line, where it shows Index: 393, Size: 393, what does that mean? The index and size of the array?

    Let's go to the code:

    1.- I scrap more than 2.700 links that are saved in an array called all_links. As I want to store a lot of information I am using a bidimensional ArrayList called listaEmpresaA:

    Java Code:
    ArrayList<ArrayList<String>> listaEmpresaA = new ArrayList<ArrayList<String>>();
    
        String [] paises = {"USA"};
    
        int total_columnas = 2 + (paises.length*3);
    
        //CREATING THE COLUMS
         for(int i =0; i< total_columnas; i++){
                    listaEmpresaA.add(new ArrayList<String>());
                }
    
         //DEFINITION OF THE ROWS
    
    
         //<--------------- START OF THE HEADER DEFINITION
    
         listaEmpresaA.get(0).add("Juego");
         listaEmpresaA.get(1).add("URL");
    
    
    
         for (z=0 ; z<paises.length; z++) {
             for (int j=2; j<total_columnas ; j=j+3 ) {
                 listaEmpresaA.get(j).add(paises[z]);
                 listaEmpresaA.get(j+1).add(paises[z] + " Gold");
                 listaEmpresaA.get(j+2).add(paises[z] + " sin Gold");
             }
         }
    
        int filas = 1; //JUST TO KNOW THE AMOUNT OF ROWS I HAVE
    
         //<--------------- FINISH OF THE HEADER DEFINITION
    
    
        //<--------------- STARTING OF THE SCRAPING FOR EACH LINK
    
        int contador_juegos = 1;
    
        for (String link : all_links) {
    
         String urlPage = "https://www.microsoft.com" + link;
         System.out.println(contador_juegos + ".- Comprobando entradas de: "+urlPage);
    
         if (getStatusConnectionCode(urlPage) == 200) {
    
             Document document = getHtmlDocument(urlPage);
    
             Elements entradas = document.select("div.page-header div.m-product-detail-hero-product-placement div.context-product-placement-data");
    
             for (Element elem : entradas) {
                 String titulo = elem.getElementsByClass("c-heading-2").text();
    
                 System.out.println(titulo+"\n");
                 listaEmpresaA.get(0).add(titulo);
                 listaEmpresaA.get(1).add(urlPage);
    
             }
    
             entradas = document.select("div.price-info");
    
             for (Element elem : entradas) {
                 String titulo = elem.getElementsByTag("s").text();
    
                 System.out.println("Precio base: " + titulo+"\n");
                 listaEmpresaA.get(2).add(titulo);
    
             }
    
             entradas = document.select("div.price-info");
    
             for (Element elem : entradas) {
                 String titulo = elem.getElementsByClass("price-disclaimer").text();
    
                 System.out.println("Precio para los miembros sin GOLD: " + titulo+"\n");
                 listaEmpresaA.get(3).add(titulo);
    
    
             }
    
             entradas = document.select("dd.cli_upsell-options div.cli_upsell-option");
    
             // Paseo cada una de las entradas
             for (Element elem : entradas) {
                 String titulo = elem.getElementsByClass("price-disclaimer").text();
    
                 System.out.println("Precio para los miembros GOLD: " + titulo+"\n");
                 listaEmpresaA.get(4).add(titulo);
    
             }
    
             filas++;
    
         }
    
         contador_juegos++;
        }
    
    
        //<--------------- FINISH OF THE SCRAPING FOR EACH LINK BAZAR USA
    2.- Create the Excel and store the information from listaEmpresaA arrayList to the Excel.

    Java Code:
    try {
             //create .xls and create a worksheet.
             FileOutputStream fos = new FileOutputStream("D:\\mierda.xls");
             HSSFWorkbook workbook = new HSSFWorkbook();
             HSSFSheet worksheet = workbook.createSheet("XboxOne");
    
                int l=0;
    
                    //CREATING EXCEL ROWS
                 for (int f=0; f< filas ; f++) {
                    HSSFRow fila = worksheet.createRow(f);
    
                    //CREATING EXCEL COLUMNS
                    for(int c=0;c<total_columnas;c++){
                           HSSFCell celda = fila.createCell(c);
                           celda.setCellValue(listaEmpresaA.get(c).get(f)); //<----- THIS IS THE LINE 305 WHERE I HAVE THE ERROR
                           l++;
    
                    }      
                 }
    
            //Save the workbook in .xls file
             workbook.write(fos);
             fos.flush();
             fos.close();
         } catch (FileNotFoundException e) {
             e.printStackTrace();
         } catch (IOException e) {
             e.printStackTrace();
         }
    It seems to be a problem with the generation of the Excel but i can't find the problem my-self.

    I have many questions and I would really appreciate if you could give me some tips so that I can find the solution my-self:

    1.- I don't understand why it is shown the problem at this point Index: 393, Size: 393 when the program has been running until the index 2730 of the total links (2.751 links in total). The last data shown on the console is:

    XML Code:
    2730.- Comprobando entradas de: https://www.microsoft.com/en-us/store/p/star-wars-pinball-season-1-bundle/brz3mqfjnlmw Star Wars™ Pinball Season 1 Bundle
    
    Precio base:
    
    Precio para los miembros sin GOLD:
    
    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 393, Size: 393 at java.util.ArrayList.rangeCheck(ArrayList.java:653) at java.util.ArrayList.get(ArrayList.java:429) at scraping.complementos_juegos.main(complementos_juegos.java:305)
    2.- When I use the for-each bucle, I have realized that it is not exactly following the order set on the array. I don't know why.

    3.- The program spends 1 hour just to scrap information from one store, and I want to store more than 50, ¿is there a way to reduce this time? I have read something about "HashMap" but I don't know how to use them. Anyway if it is a better solution I will take a look.

    Thanks in advance!

    Have a good day.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 393, Size: 393
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at scraping.complementos_juegos.main(complementos_jue gos.java:305)
    The statement at line 305 used an index value of 393 in an ArrayList with 393 items. Remember array indexes range in value from 0 to the length-1.
    The max index for an array with 393 items is 392.
    If you don't understand my response, don't ignore it, ask a question.

  3. #3
    JetLagFox is offline Member
    Join Date
    Jan 2017
    Posts
    15
    Rep Power
    0

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Quote Originally Posted by Norm View Post
    The statement at line 305 used an index value of 393 in an ArrayList with 393 items. Remember array indexes range in value from 0 to the length-1.
    The max index for an array with 393 items is 392.
    Thanks for your response,

    I have that in mind, but seems to be something more. I have been trying giving different values for the array and it is nonsense. I would understand if just by adding as you said length-1 in the for condicional worked, but is not working...I don't know what to do.

    I have been giving different values just changing a little bit the code:

    Java Code:
    int contador_juegos = 1;
    		
    				
    		for (z=0; z<10 ; z++) {
    					 
             String urlPage = "https://www.microsoft.com" + all_links.get(z); 
             System.out.println(contador_juegos + ".- Comprobando entradas de: "+urlPage);
    			
             // Compruebo si me da un 200 al hacer la petición
             if (getStatusConnectionCode(urlPage) == 200) {
    				
                 // Obtengo el HTML de la web en un objeto Document2
                 Document document = getHtmlDocument(urlPage);
    				
                 // Busco todas las historias de meneame que estan dentro de: 
                 Elements entradas = document.select("div.page-header div.m-product-detail-hero-product-placement div.context-product-placement-data");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByClass("c-heading-2").text();
                
                     System.out.println(titulo+"\n");
                     listaEmpresaA.get(0).add(titulo);
                     listaEmpresaA.get(1).add(urlPage);
    					
                 }
                 
                 entradas = document.select("div.price-info");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByTag("s").text();
                
                     System.out.println("Precio base: " + titulo+"\n");
                     listaEmpresaA.get(2).add(titulo);
    					
                 }
                 
                 entradas = document.select("div.price-info");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByClass("price-disclaimer").text();
                
                     System.out.println("Precio para los miembros sin GOLD: " + titulo+"\n");
                     listaEmpresaA.get(3).add(titulo);
                    
    					
                 }
                 
                 entradas = document.select("dd.cli_upsell-options div.cli_upsell-option");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByClass("price-disclaimer").text();
                
                     System.out.println("Precio para los miembros GOLD: " + titulo+"\n");
                     listaEmpresaA.get(4).add(titulo);
    					
                 }
                 
                 filas++;
    		
             }
            
    	
    		 
             contador_juegos++;
        }
    Instead of going through the arrayList I´m using ant integer type variable for the condicional. Also when creating the Excel so that I don't have to wait an hour to have the results.

    Java Code:
    try {
                 //create .xls and create a worksheet.
                 FileOutputStream fos = new FileOutputStream("D:\\mierda.xls");
                 HSSFWorkbook workbook = new HSSFWorkbook();
                 HSSFSheet worksheet = workbook.createSheet("XboxOne");
                 
                 //Escribimos en el Excel toda la información
                	int l=0;
                	
                		//Recorremos las filas
                     for (int f=0; f< 10 ; f++) {
                     	HSSFRow fila = worksheet.createRow(f);
                     	
                     	//Recorremos las columnas
                     	for(int c=0;c<total_columnas;c++){
                     		System.out.println("F value:" + f + "<---> C value: " + c);
                     		   HSSFCell celda = fila.createCell(c);
                     		   celda.setCellValue(listaEmpresaA.get(c).get(f));
                     		   l++;
                     		   
                     	}	   
                     }
                                   
                //Save the workbook in .xls file
                 workbook.write(fos);
                 fos.flush();
                 fos.close();
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
                
        }
    But keeps working when it wants...I have been running the code mentally and with a paper and I really don't understand what is happening.

    So desperate. The only thing I think I understand is that every Excel cell has to have a value.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    is not working
    Please explain what "is not working" means. If there is an error messages, please copy it and paste it here.
    If you don't understand my response, don't ignore it, ask a question.

  5. #5
    JetLagFox is offline Member
    Join Date
    Jan 2017
    Posts
    15
    Rep Power
    0

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Quote Originally Posted by Norm View Post
    Please explain what "is not working" means. If there is an error messages, please copy it and paste it here.
    Here is the full code of an example where listaEmpresaA arrayList only has 20 rows:

    Java Code:
        public static void main (String args[]) throws IOException {
        	
        	
        	
        	//<--------------- SCRAPPING DE TODOS LOS ENLACES A LOS VIDEOJUEGOS DE XBOX ONE
        	
    
        	
        	    String url = "https://www.microsoft.com/en-us/store/top-paid/games/xbox?s=store&skipitems=";
        		
            	int juegos_totales = 0;
         	
            	for (int i=0; i<1000; i=i+90) {
                    String urlPage = url+i;
                    System.out.println("Comprobando entradas de: "+urlPage);
        			
                    // Compruebo si me da un 200 al hacer la petición
                    if (getStatusConnectionCode(urlPage) == 200) {
        				
                        // Obtengo el HTML de la web en un objeto Document2
                        Document document = getHtmlDocument(urlPage);
        				
                        // Busco todas las historias de meneame que estan dentro de: 
                        Elements entradas = document.select("section.m-product-placement-item");
        				
                       
                        
                        // Paseo cada una de las entradas
                        for (Element elem : entradas) {
                            String titulo = elem.getElementsByClass("c-heading").text();
                            juegos_totales++;
                            
                            /*System.out.println(titulo+"\n");*/
        					
                        }
                        
                        System.out.println("Hay un total de " + juegos_totales + " juegos");
        		
                    }
                    
            	}
            	
            	
            	ArrayList<String> all_links = new ArrayList<String>();
            	  	
            	int z=0;
            	
            	for (int i=0; i<1000; i=i+90) {
                    String urlPage = url+i;
        			
                    if (getStatusConnectionCode(urlPage) == 200) {
        				
                        Document document = getHtmlDocument(urlPage);
        				
                        Elements entradas = document.select("section.m-product-placement-item");
        				                    
                        // Paseo cada una de las entradas
                        
                        
                        for (Element elem : entradas) {
                        	
                        		Elements links = elem.getElementsByTag("a");
                                for (Element link : links ) {
                                	all_links.add(link.attr("href"));
                                	
                                }
                        		z++;
    
                        	}
                        		
                    }
                    
            	}
            	
            	//Recorremos cada link de cada juego base para coger todos los juegos que pertenecen a ese juego
            	//bien sea porque es un pack o porque es un complemento.
            	
            	for (int i=1; i< 20; i++) { 
            		
                String urlPage = "https://www.microsoft.com" + all_links.get(i);
                System.out.println("Comprobando entradas de: "+urlPage);
        		
                // Compruebo si me da un 200 al hacer la petición
                if (getStatusConnectionCode(urlPage) == 200) {
        			
                    // Obtengo el HTML de la web en un objeto Document2
                    Document document = getHtmlDocument(urlPage);
        			
                    
                    Elements entradas = document.select("div#pdpbundles div.c-carousel section.c-product-placement");
        			
                    // Paseo cada una de las entradas
                    for (Element elem : entradas) {
                        Elements links = elem.getElementsByTag("a");
                        for (Element link : links ) {
                        	
                        	int igual = 0;
                        	
                        	for (int j=0; j<all_links.size(); j++) {
                        		if (all_links.get(j).equalsIgnoreCase(link.attr("href"))) {
                        			igual++;
                        		}
                        	}
                        	
                        	if (igual == 0) {
                        		all_links.add(link.attr("href"));
                            	juegos_totales++;
                        	} else {
                        		igual = 0;
                        	}
                        	
                        }             
        				
                    }
                    
                    //Aquí se buscan los complementos
                    
                    entradas = document.select("div#addonswithdetails div.c-carousel ul.c-group li.slide");
        			
                    // Paseo cada una de las entradas
                    for (Element elem : entradas) {
                        Elements links = elem.getElementsByTag("a");
                        for (Element link : links ) {
                        	
                            int igual = 0;
                        	
                        	for (int j=0; j<all_links.size(); j++) {
                        		if (all_links.get(j).equalsIgnoreCase(link.attr("href"))) {
                        			igual++;
                        		}
                        	}
                        	
                        	if (igual == 0) {
                        		all_links.add(link.attr("href"));
                            	juegos_totales++;
                        	} else {
                        		igual = 0;
                        	}
                        }
                        
        				
                    }
        	
                }
            }
        	
        	
        	
            //<--------------- FIN SCRAPPING DE TODOS LOS ENLACES A LOS VIDEOJUEGOS DE XBOX ONE    
            	
            	System.out.println(all_links.size());
            	
                try {
                    //create .xls and create a worksheet.
                    FileOutputStream fos = new FileOutputStream("D:\\listado_links.xls");
                    HSSFWorkbook workbook = new HSSFWorkbook();
                    HSSFSheet worksheet = workbook.createSheet("XboxOne");
                    
                    //Escribimos en el Excel toda la información
                   	int l=0;
                   	
                   		//Recorremos las filas
                        for (int f=0; f< all_links.size() ; f++) {
                        	HSSFRow fila = worksheet.createRow(f);
                        	
                        	//Recorremos las columnas
                        	for(int c=0;c<1;c++){
                        		   HSSFCell celda = fila.createCell(c);
                        		   celda.setCellValue(all_links.get(l));
                        		   l++;
                        		   
                        	}	   
                        }
    
                	
                	
                	System.out.println("Hay un total de " + juegos_totales + " enlaces");
                    //Create ROW-1
                	
    
                                         
                   //Save the workbook in .xls file
                    workbook.write(fos);
                    fos.flush();
                    fos.close();
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        	
        	
        	
        	
        	ArrayList<ArrayList<String>> listaEmpresaA = new ArrayList<ArrayList<String>>();
    		
    		String [] paises = {"USA"};
    		
    		int total_columnas = 2 + (paises.length*3);
    		
    		//Aquí creas las columnas
    		 for(int i =0; i< total_columnas; i++){
    		            listaEmpresaA.add(new ArrayList<String>());
    		        }
    		 
    		 //Aquí se definen las filas
    		 
    		 	
    		 //Creo la cabecera
    		 
    		 listaEmpresaA.get(0).add("Juego");
    		 listaEmpresaA.get(1).add("URL");
    		 		 
    		 
    		 for (z=0 ; z<paises.length; z++) {
    			 for (int j=4; j<total_columnas ; j=j+3 ) {
    				 listaEmpresaA.get(j-2).add(paises[z]);
    				 listaEmpresaA.get(j-1).add(paises[z] + " Gold");
    				 listaEmpresaA.get(j).add(paises[z] + " sin Gold");
    			 }
    		 }
    		 
    		int filas = 1; //Recuento de las filas
    		 
    		 //Fin de la creación de la cabecera.
    		
    		
            //<--------------- COMIENZO DEL SCRAPING PARA CONSEGUIR LOS PRECIOS DEL BAZAR USA
    		
    		int contador_juegos = 1;
    		
    				
    		for (z=0; z<20 ; z++) {
    					 
             String urlPage = "https://www.microsoft.com" + all_links.get(z); 
             System.out.println(contador_juegos + ".- Comprobando entradas de: "+urlPage);
    			
             // Compruebo si me da un 200 al hacer la petición
             if (getStatusConnectionCode(urlPage) == 200) {
    				
                 // Obtengo el HTML de la web en un objeto Document2
                 Document document = getHtmlDocument(urlPage);
    				
                 // Busco todas las historias de meneame que estan dentro de: 
                 Elements entradas = document.select("div.page-header div.m-product-detail-hero-product-placement div.context-product-placement-data");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByClass("c-heading-2").text();
                
                     System.out.println(titulo+"\n");
                     listaEmpresaA.get(0).add(titulo);
                     listaEmpresaA.get(1).add(urlPage);
    					
                 }
                 
                 entradas = document.select("div.price-info");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByTag("s").text();
                
                     System.out.println("Precio base: " + titulo+"\n");
                     listaEmpresaA.get(2).add(titulo);
    					
                 }
                 
                 entradas = document.select("div.price-info");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByClass("price-disclaimer").text();
                
                     System.out.println("Precio para los miembros sin GOLD: " + titulo+"\n");
                     listaEmpresaA.get(3).add(titulo);
                    
    					
                 }
                 
                 entradas = document.select("dd.cli_upsell-options div.cli_upsell-option");
    				
                 // Paseo cada una de las entradas
                 for (Element elem : entradas) {
                     String titulo = elem.getElementsByClass("price-disclaimer").text();
                
                     System.out.println("Precio para los miembros GOLD: " + titulo+"\n");
                     listaEmpresaA.get(4).add(titulo);
    					
                 }
                 
                 filas++;
    		
             }
            
    	
    		 
             contador_juegos++;
        }
    		
             
             
             
            //<--------------- FIN DEL SCRAPING PARA CONSEGUIR LOS PRECIOS DEL BAZAR USA        
             
             
             System.out.println("Total de filas: " + filas);
             
    		 try {
                 //create .xls and create a worksheet.
                 FileOutputStream fos = new FileOutputStream("D:\\mierda.xls");
                 HSSFWorkbook workbook = new HSSFWorkbook();
                 HSSFSheet worksheet = workbook.createSheet("XboxOne");
                 
                 //Escribimos en el Excel toda la información
                	int l=0;
                	
                		//Recorremos las filas
                     for (int f=0; f< filas ; f++) {
                     	HSSFRow fila = worksheet.createRow(f);
                     	
                     	//Recorremos las columnas
                     	for(int c=0;c<total_columnas;c++){
                     		System.out.println("F value:" + f + "<---> C value: " + c);
                     		   HSSFCell celda = fila.createCell(c);
                     		   celda.setCellValue(listaEmpresaA.get(c).get(f)); //HERE IS WHERE I GET THE ERROR
                     		   l++;
                     		   
                     	}	   
                     }
                                   
                //Save the workbook in .xls file
                 workbook.write(fos);
                 fos.flush();
                 fos.close();
             } catch (FileNotFoundException e) {
                 e.printStackTrace();
             } catch (IOException e) {
                 e.printStackTrace();
             }
                
        }


    And the error I am given is the following:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 17, Size: 17
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at scraping.solo_diez.main(solo_diez.java:348)



    And that´s what I don´t understand, why the index 17? Because if I change the limit of listaEmpresaA arraylist to 40, I am given the following error:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 34, Size: 34
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at scraping.solo_diez.main(solo_diez.java:348)


    Why 34 and not 17? The arraylist is just bigger but I hasn't changed the first 20 rows.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    change the limit of listaEmpresaA arraylist to 40
    Is the 40 the initial capacity value passed to the ArrayList's constructor? That value is not the current contents of the ArrayList which is determined by the number of items added to the ArrayList. The initial capacity is the number of slots available in the ArrayList before it will have to be resized to accept more elements.

    The code should always check that it does not attempt to access any elements past the end of the list which is at the list's length-1
    If you don't understand my response, don't ignore it, ask a question.

  7. #7
    JetLagFox is offline Member
    Join Date
    Jan 2017
    Posts
    15
    Rep Power
    0

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Quote Originally Posted by Norm View Post
    Is the 40 the initial capacity value passed to the ArrayList's constructor? That value is not the current contents of the ArrayList which is determined by the number of items added to the ArrayList. The initial capacity is the number of slots available in the ArrayList before it will have to be resized to accept more elements.

    The code should always check that it does not attempt to access any elements past the end of the list which is at the list's length-1
    Thanks for your response,

    i´m sorry but I don't understand the question as I should, I'm from spain and is being difficult to understand due to technical vocabulary and that I started learning Java on the 1st of january. 40 is just a limit I have put to the for conditional so that I can see its behavior instead of using the for conditional with the full arraylist, because It spends one hour to run it.

    I don't really understand what you mean, the arraylist is fully completed when before I start creating the Excel. If I´m not wrong, the rest of the Excel is OK, the problems seems to be from the line 308 to 328, to be more exact, the line 325 which corresponds to this line: celda.setCellValue(listaEmpresaA.get(c).get(f));

    Manually doing as:

    filas = 41;
    total_columnas = 5;

    The results for the conditionals:

    celda.setCellValue(listaEmpresaA.get(0).get(0));
    celda.setCellValue(listaEmpresaA.get(1).get(0));
    celda.setCellValue(listaEmpresaA.get(2).get(0));
    celda.setCellValue(listaEmpresaA.get(3).get(0));
    celda.setCellValue(listaEmpresaA.get(4).get(0));

    celda.setCellValue(listaEmpresaA.get(0).get(1));
    celda.setCellValue(listaEmpresaA.get(1).get(1));
    celda.setCellValue(listaEmpresaA.get(2).get(1));
    celda.setCellValue(listaEmpresaA.get(3).get(1));
    celda.setCellValue(listaEmpresaA.get(4).get(1));

    celda.setCellValue(listaEmpresaA.get(0).get(2));
    celda.setCellValue(listaEmpresaA.get(1).get(2));
    celda.setCellValue(listaEmpresaA.get(2).get(2));
    celda.setCellValue(listaEmpresaA.get(3).get(2));
    celda.setCellValue(listaEmpresaA.get(4).get(2));

    ....
    ....
    ....

    celda.setCellValue(listaEmpresaA.get(0).get(40)); // value: Grand Theft Auto V & Megalodon Shark Cash Card Bundle
    celda.setCellValue(listaEmpresaA.get(1).get(40)); // value: https://www.microsoft.com/en-us/stor...e/c54tnmrcbh0c
    celda.setCellValue(listaEmpresaA.get(2).get(40)); // value: $135.99
    celda.setCellValue(listaEmpresaA.get(3).get(40)); // value: $68.00 +
    celda.setCellValue(listaEmpresaA.get(4).get(40)); // NO VALUE

    As you can see there is a problem with the last index in the last position of the column...but why? The rest is OK...

    The error it throws:

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 40, Size: 34
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at scraping.solo_diez.main(solo_diez.java:340)

    So i have taken also the results for the following positions:
    System.out.println(listaEmpresaA.get(4).get(33)); // $54.40+*
    System.out.println(listaEmpresaA.get(4).get(34)); // NO VALUE
    System.out.println(listaEmpresaA.get(4).get(35)); // NO VALUE
    System.out.println(listaEmpresaA.get(4).get(36)); // NO VALUE
    System.out.println(listaEmpresaA.get(4).get(37)); // NO VALUE
    System.out.println(listaEmpresaA.get(4).get(38)); // NO VALUE
    System.out.println(listaEmpresaA.get(4).get(39)); // NO VALUE

    I don't understand why there is a problem with the las column and not with the others...I have tried with the 3 index column and no problem...so so lost.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    From the API doc for the ArrayList class:
    An application can increase the capacity of an ArrayList instance before adding a large number of elements using the ensureCapacity operation. This may reduce the amount of incremental reallocation.
    Changing the size of an ArrayList's internal array and copying elements from the old to the new array can be expensive. When you know there will be a large number of items added to the ArrayList, Setting the internal capacity will save the cost of resizing and copying.

    Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 40, Size: 34
    at java.util.ArrayList.rangeCheck(ArrayList.java:653)
    at java.util.ArrayList.get(ArrayList.java:429)
    at scraping.solo_diez.main(solo_diez.java:340)
    Again the code tried to access an element (40) past the end of what is in the ArrayList (34). The maximum index in an ArrayList with 34 elements is 33.

    Make sure the code always checks the index it uses will not go past the end of the ArrayList.
    Every call to the add() method increases the number of elements in the ArrayList. The size() method Returns the number of elements in this list.
    If you don't understand my response, don't ignore it, ask a question.

  9. #9
    JetLagFox is offline Member
    Join Date
    Jan 2017
    Posts
    15
    Rep Power
    0

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Quote Originally Posted by Norm View Post
    From the API doc for the ArrayList class:

    Changing the size of an ArrayList's internal array and copying elements from the old to the new array can be expensive. When you know there will be a large number of items added to the ArrayList, Setting the internal capacity will save the cost of resizing and copying.


    Again the code tried to access an element (40) past the end of what is in the ArrayList (34). The maximum index in an ArrayList with 34 elements is 33.

    Make sure the code always checks the index it uses will not go past the end of the ArrayList.
    Every call to the add() method increases the number of elements in the ArrayList. The size() method Returns the number of elements in this list.
    I finally solved the problem using a bidimensional array instead of arraylist. Now I'm not getting any error, but I keep doing something wrong. I'm saving all the data in the same row, or at least is what it seems.

    Edit: I have found the problem, I was using a variable that wasn't been changed. I'm running the code again but is should work.
    Last edited by JetLagFox; 01-10-2017 at 12:02 PM. Reason: solved the problem

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    5,847
    Rep Power
    10

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Quote Originally Posted by JetLagFox View Post
    I finally solved the problem using a bidimensional array instead of arraylist.
    Wife: Dear, I am having a problem with the dishwasher, will you fix it?
    Husband: Your problem is solved, you need to hand wash.

    In other words, you did not solve the problem. You simply chose a different method of accomplishing the task.

    Lists are preferred over arrays. You should use them. And remember the following:

    1. An ArrayList is backed by an array. So if you can't do it to an Array, you probably can't do it to an ArrayList.
    2. The capacity is the maximum number of elements that the list can hold before it internally resizes them. So you really don't have to worry about this.
    3. List.size() is the number of items in the list, indexed from 0 to size() -1.
    4. You cannot randomly add or retrieve elements at indices >= size(). If size() == 4 and you try to set item 20 to something you will get an error. You can only add to the end of a list.

    And in problems like you are experiencing, you need to sprinkle print statements near the statements causing the error to see the values of important variables.

    Regards,
    Jim
    Last edited by jim829; 01-10-2017 at 06:23 PM. Reason: clarification - item 4
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    JetLagFox is offline Member
    Join Date
    Jan 2017
    Posts
    15
    Rep Power
    0

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Quote Originally Posted by jim829 View Post
    Wife: Dear, I am having a problem with the dishwasher, will you fix it?
    Husband: Your problem is solved, you need to hand wash.

    In other words, you did not solve the problem. You simply chose a different method of accomplishing the task.

    Lists are preferred over arrays. You should use them. And remember the following:

    1. An ArrayList is backed by an array. So if you can't do it to an Array, you probably can't do it to an ArrayList.
    2. The capacity is the maximum number of elements that the list can hold before it internally resizes them. So you really don't have to worry about this.
    3. List.size() is the number of items in the list, indexed from 0 to size() -1.
    4. You cannot add or retrieve elements out of order. If size() == 4 and you try to set item 20 to something you will get an error. You can only add to the end of a list.

    And in problems like you are experiencing, you need to sprinkle print statements near the statements causing the error to see the values of important variables.

    Regards,
    Jim
    Thanks for your response,

    Well could be another way to say but I couldn't deal with the bidimensional arraylist, I'm not able to understand them. I'm using arraylist for "all_links" because I don't really know the amount of links I will get, but once I know that quantity I know the size for the bidimensional array, and I don't have problems to use them because I'm pretty used to use bidimensional arrays in PHP. With that change in the code I'm getting all the data.

    Another thing I should do is use modular estructure.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    19,624
    Rep Power
    29

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Perhaps your orientation of row and column is a problem. I think of 2 dimenions arrays as having rows with columns.
    The code in post#1 seems to do it the other way: columns with rows:
    Java Code:
        //CREATING THE COLUMS
         for(int i =0; i< total_columnas; i++){
                    listaEmpresaA.add(new ArrayList<String>());
                }
    Here's sample code that creates a two dim arraylist with each row being an arraylist, and the contents of that row being the columns which hold data:
    Java Code:
          ArrayList<ArrayList<String>> twoDimAL = new ArrayList<ArrayList<String>>();
          // build a 2 dim arraylist with 4 rows and 2 columns in each row
          for(int row=0; row < 4; row++) {
             twoDimAL.add(new ArrayList<String>()); //  add empty AL on this row
             // fill the columns on this row
             for(int col=0; col < 2; col++) {
               twoDimAL.get(row).add("{r="+row +" c="+col+"}");   // add data to column in this row
             }
           }
           // Show contents of AL
           System.out.println(twoDimAL);//[[{r=0 c=0}, {r=0 c=1}], [{r=1 c=0}, {r=1 c=1}], [{r=2 c=0}, {r=2 c=1}], [{r=3 c=0}, {r=3 c=1}]]
    
           // access item on row 2 and column 1
           System.out.println(twoDimAL.get(2).get(1));   // {r=2 c=1}
    Last edited by Norm; 01-10-2017 at 06:16 PM.
    If you don't understand my response, don't ignore it, ask a question.

  13. #13
    JetLagFox is offline Member
    Join Date
    Jan 2017
    Posts
    15
    Rep Power
    0

    Default Re: Exception in thread “main” java.lang.IndexOutOfBoundsException in Java

    Quote Originally Posted by Norm View Post
    Perhaps your orientation of row and column is a problem. I think of 2 dimenions arrays as having rows with columns.
    The code in post#1 seems to do it the other way: columns with rows:
    Java Code:
        //CREATING THE COLUMS
         for(int i =0; i< total_columnas; i++){
                    listaEmpresaA.add(new ArrayList<String>());
                }
    Here's sample code that creates a two dim arraylist with each row being an arraylist, and the contents of that row being the columns which hold data:
    Java Code:
          ArrayList<ArrayList<String>> twoDimAL = new ArrayList<ArrayList<String>>();
          // build a 2 dim arraylist with 4 rows and 2 columns in each row
          for(int row=0; row < 4; row++) {
             twoDimAL.add(new ArrayList<String>()); //  add empty AL on this row
             // fill the columns on this row
             for(int col=0; col < 2; col++) {
               twoDimAL.get(row).add("{r="+row +" c="+col+"}");   // add data to column in this row
             }
           }
           // Show contents of AL
           System.out.println(twoDimAL);//[[{r=0 c=0}, {r=0 c=1}], [{r=1 c=0}, {r=1 c=1}], [{r=2 c=0}, {r=2 c=1}], [{r=3 c=0}, {r=3 c=1}]]
    
           // access item on row 2 and column 1
           System.out.println(twoDimAL.get(2).get(1));   // {r=2 c=1}
    Yes its probably the main reason, anyway in this case I don't have problems to use an array with a defined length. I read something about that arraylist are a little bit slower, just miliseconds, even the performance is almost the same.

    That sample is very clear.

Similar Threads

  1. Replies: 3
    Last Post: 11-11-2013, 03:30 AM
  2. Replies: 5
    Last Post: 04-22-2012, 04:59 PM
  3. Exception in thread main java.lang.nosuchmethoderror main
    By prikesh_joshi7 in forum Java Applets
    Replies: 3
    Last Post: 01-26-2012, 05:19 PM
  4. Replies: 1
    Last Post: 11-23-2011, 06:52 AM
  5. Replies: 3
    Last Post: 02-03-2011, 05:49 PM

Tags for this Thread

Posting Permissions

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