此抓取程序超出范围异常 怎么解决?
线程中的异常" main" java.lang.IndexOutOfBoundsException:索引: 11,大小:11 at java.util.ArrayList.rangeCheck(ArrayList.java:653) 在java.util.ArrayList.get(ArrayList.java:429)at 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);
}
}
}
答案 0 :(得分:0)
以下是代码的爆炸式增长:
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 的大小进行循环,但不检查 bl 或 cl 的大小......
答案 1 :(得分:0)
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行放置一个断点并进行调试。您的调试器肯定有可能在运行时查找所有值。
答案 2 :(得分:0)
a1,b1,c1你必须确保b1,c1和至少相同大小或大于a1.size()。调整你的逻辑acc.means迭代语句应该在三个中的最小值之间arraylists.use使用类似下面的内容来查找给出arraylists之间的最小大小并使用该值来启动你的循环。你得到的错误是在下面的代码中生成你将indexout的绑定,因为显然b1和c1小于a1和你的i index将尝试获取b1.get(2),c2.get(2),它显然没有任何值
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));
}
}
public class TestArrayList {
<T> void save(T o) {
@DatabaseTable
class wrap extends T {
@DatabaseField(generatedId = true)
public Long id;
}
try {
Dao<T, Long> d = (Dao<T, Long>) getClassDao(o.getClass());
TableUtils.createTableIfNotExists(connectionSource, o.getClass());
d.createOrUpdate(o);
} catch (Exception e) {
throw new RuntimeException(e);
}
}
}