【问题标题】:Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11线程“主”java.lang.IndexOutOfBoundsException 中的异常:索引:11,大小:11
【发布时间】:2017-01-02 23:08:07
【问题描述】:

这个爬虫程序出现越界异常 如何解决?

线程“主”java.lang.IndexOutOfBoundsException 中的异常:索引: 11,大小:11 在 java.util.ArrayList.rangeCheck(ArrayList.java:653) 在 java.util.ArrayList.get(ArrayList.java:429) 在 Glowship_inv_battery.main(Glowship_inv_battery.java:68)

代码:

import java.util.ArrayList;

import com.webscrap4j.WebScrap;
import com.webscrap4j.WebScrapException;

import java.io.FileOutputStream;
import java.io.IOException;

import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Row;
  
public class letsbuybattery_inverterbattery 
{
    @SuppressWarnings("resource")
    public static void main(String[] args) throws IOException {
    
        ArrayList<String> al = new ArrayList<String>();
        ArrayList<String> bl = new ArrayList<String>();
        ArrayList<String> cl = new ArrayList<String>();
        WebScrap ws = new WebScrap();
        
        ws.setUrl("http://letsbuybattery.com/inverters/");
        try
        {               
            ws.startWebScrap();
            
            al = ws.getSingleHTMLScriptData("<h5>", "</h5>");
            //mrp
            bl = ws.getSingleHTMLScriptData("<div class='clearfix'> ", "</div>");
            //selling price
            cl = ws.getSingleHTMLScriptData("<div class='item-price pull-left'>", "</div>");                
            
            HSSFWorkbook workBook = new HSSFWorkbook();
             FileOutputStream fos = new FileOutputStream("/Users/parthpatil/Documents/Abm Technologies/Crawl/letbuybattery_battery.xls"); 
             {

            // Create the Sheet
            HSSFSheet Sheet = workBook.createSheet("products");

            // Create the first row corresponding to the header
            Row header = Sheet.createRow(0);
            header.createCell(0).setCellValue("Product Name");
            header.createCell(1).setCellValue("Product Price");
            header.createCell(2).setCellValue("Product MRP");

            // Ensure that all the List have the same size otherwise throw an exception
            //  if (al.size() != bl.size() || al.size() != cl.size())
            //    throw new IllegalStateException("Some data is missing");

            // Iterate over all the list an create the rows of data
            for(int i = 0; i < al.size(); i++){
                // Create the current starting from 1 to al.size()
                HSSFRow row = Sheet.createRow((short) i + 1);
                // Cell of the Product Name
                row.createCell(0).setCellValue(al.get(i));
                // Cell of the Product Price
                row.createCell(1).setCellValue(cl.get(i));
                // Cell of the Product MRP
                row.createCell(2).setCellValue(bl.get(i));
                
            }
            // Write the result into the file
            workBook.write(fos);
            
            for (String adata : al)
            {    
                System.out.println("the product are:- " + adata);    
            }

            for (String bdata : bl)
            {
                System.out.println("the MRp are:- " + bdata);    
            }

            for (String cdata : cl)
            {    
                System.out.println("the selling price is:- " + cdata);    
            }               
        }
             }catch (WebScrapException e) {
            
            e.printStackTrace(System.out);
        }
        }
    }

【问题讨论】:

  • 你确定 al, bl 和 cl 大小一样????
  • 你的 al, bl, cl 必须有不同的尺寸。调试你的代码,或者在循环之前打印尺寸。
  • 原因是您正在访问索引从 0 到 10 的数组的第 11 个索引。
  • @balmy 你不应该在问题中修改人们的代码,请参阅meta.stackoverflow.com/q/260245/525036。我回滚了这个,但我看到你做的更多,所以我不会发起编辑战。

标签: java arraylist indexoutofboundsexception


【解决方案1】:

a1,b1,c1 您必须确保 b1,c1 和 a1.size() 的大小至少相同或大于 a1.size().adjust your logic accrodingly.means 迭代语句应该在三个中的最小值arraylists.use 类似下面的东西来找到数组列表中的最小大小并使用该值来启动你的循环。你得到的错误是在下面的代码中产生的,你将 indexout of bound 因为显然 b1 和 c1 小于 a1 并且你的 i index 将尝试获取 b1.get(2),c2.get(2) 显然没有任何价值

    int d = c1.size() < (a1.size()< b1.size() ? a1.size() : b1.size()) ? c1.size() : ((a1.size() < b1.size()) ? a1.size() : b1.size());
public class TestArrayList {

    public static void main(String[] args) {
        // TODO Auto-generated method stub

        
        ArrayList<String> a1=new ArrayList<>();
        ArrayList<String> b1=new ArrayList<>();
        ArrayList<String> c1=new ArrayList<>();
        a1.add("a1");
        a1.add("a2");
        a1.add("a3");
        
        b1.add("b1");
        b1.add("b2");
        
        c1.add("c1");
        c1.add("c2");
        
        for(int j=0;j<a1.size();j++)
        {
            System.out.println("finding thestuuf from arralist to use in your excel cells"+">>>"+a1.get(j)+">>>"+b1.get(j)+">>>"+c1.get(j));
        }
        
    }

}

【讨论】:

  • 感谢您的帮助,我明白了这一点并更改了我的代码,这里页面有很多空白标签,所以不得不跳过 bl 并且它有效
【解决方案2】:

这里是代码爆炸发生的:

 for(int i = 0; i < al.size(); i++){
                // Create the current starting from 1 to al.size()
                HSSFRow row = Sheet.createRow((short) i + 1);
                // Cell of the Product Name
                row.createCell(0).setCellValue(al.get(i));
                // Cell of the Product Price
                row.createCell(1).setCellValue(cl.get(i));
                // Cell of the Product MRP
                row.createCell(2).setCellValue(bl.get(i));
            }

您正在循环使用 al 的大小,但没有检查 blcl 的大小...

【讨论】:

  • al ,bl 和 cl 的大小不同,但重要的是名称,所以我评论了一些数据丢失异常
【解决方案3】:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 11, Size: 11 
at java.util.ArrayList.rangeCheck(ArrayList.java:653) 
at java.util.ArrayList.get(ArrayList.java:429) 
at Glowship_inv_battery.main(Glowship_inv_battery.java:68)

让我们把它翻译成简单的英语:

There was an Exception thrown because of accessing notexisting index which is 11 
    on an object which has size 11, so has indexes from 0 to 10.
It has occured on line 653 of ArrayList.java, in method rangeCheck,
    which was invoked from:
line 429 of ArrayList.java, in method get
    which was invoked from
line 68 of Glowship_inv_battery.java in method main

可能的解决方案。在Glowship_inv_battery.java 文件的第 68 行设置断点并进行调试。您的调试器肯定有可能在运行时查找所有值。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-10-29
    • 2016-01-12
    • 2013-11-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多