Java MySQL IndexOutOfBounds错误

时间:2012-09-21 20:24:40

标签: java mysql jsp servlets

为什么我收到此错误:

java.lang.IndexOutOfBoundsException:Index:4,Size:4

我没有定义任何东西,并且在另一个工作正常的servlet上使用了相同的逻辑。在我的其他servlet中,我选择了所有产品,所以我使用了两个arraylists:一个在另一个内部。我试过这里,但仍然有同样的错误。我清楚地理解错误,但我不知道如何在这种语法中解决它。

谢谢。

ConnectionPool pool = ConnectionPool.getInstance();
Connection connection = pool.getConnection();
PreparedStatement ps = null;
ResultSet rs = null;

int product_id =Integer.parseInt(req.substring(req.lastIndexOf("/")+1));  

ArrayList al = null;
String query = "select * from Product where product_id="+product_id;

try {
    ps = connection.prepareStatement(query);
    rs = ps.executeQuery(query);

    while (rs.next()) {
        al = new ArrayList();

        al.add(rs.getString("product_id"));
        al.add(rs.getString("product_name"));
        al.add(rs.getString("product_description"));
        al.add(rs.getDouble("product_price"));
    }

此servlet的下一个JSP页面是:

    <%! 
        String product_id=""; 
        String product_name=""; 
        String product_description=""; 
        double product_price = 0; 
    ArrayList  productList=null; 
    %> 

    <% 
    if(request.getAttribute("productList")!=null && request.getAttribute("productList")!="") { 
        productList = (ArrayList)request.getAttribute("productList"); 
        product_id = productList.get(1).toString(); 
        product_name = productList.get(2).toString(); 
        product_description = productList.get(3).toString(); 
        product_price = (Double) productList.get(4);
    } 
    %>

4 个答案:

答案 0 :(得分:5)

你可能在这一行得到了这个例外

int product_id =Integer.parseInt(req.substring(req.lastIndexOf("/")+1));
从您的代码

,这可能是唯一的情况。当你在req上调用subString()时索引越界。最好的测试是将sysout放入req的长度

发布STACKTRACE后编辑

将您的Java代码更改为此           al = new ArrayList();
     while(rs.next()){         al.add(rs.getString( “PRODUCT_ID”)); //存储在索引0处的al中         al.add(rs.getString(“product_name”)); //存储在索引1处的al中         al.add(rs.getString(“product_description”)); //存储在索引2处的al中         al.add(rs.getDouble(“product_price”)); //存储在索引3的al中     }

继承人抛出异常的地方

 line1:  product_id = productList.get(1).toString(); 
 line2:               product_name = productList.get(2).toString(); 
 line3:               product_description = productList.get(3).toString(); 
 line4               product_price = (Double) productList.get(4); /// its going indexoutfbound    here

你的列表只有4个元素,你试图用上面的代码获得第5个元素。      }

将其更改为

  product_id = productList.get(0).toString(); 
                product_name = productList.get(1).toString(); 
                product_description = productList.get(2).toString(); 
                product_price = (Double) productList.get(3);
} 

答案 1 :(得分:3)

int product_id =Integer.parseInt(req.substring(req.lastIndexOf("/")+1));

如果您有这样的网址: http://www.yahoo.com/

lastIndexOf会给你21和+1给出22超出范围。

修改 粘贴servlet页面后,我注意到你似乎对访问arrayList的索引感到困惑。 你从0而不是1 IIRC开始。所以:

if(request.getAttribute("productList")!=null && request.getAttribute("productList")!="") 
    { 
                    productList = (ArrayList)request.getAttribute("productList"); 
                    product_id = productList.get(1).toString(); 
                    product_name = productList.get(2).toString(); 
                    product_description = productList.get(3).toString(); 
                    product_price = (Double) productList.get(4);
    } 

 if(request.getAttribute("productList")!=null && request.getAttribute("productList")!="") 
        { 
                        productList = (ArrayList)request.getAttribute("productList"); 
                        product_id = productList.get(0).toString(); 
                        product_name = productList.get(1).toString(); 
                        product_description = productList.get(2).toString(); 
                        product_price = (Double) productList.get(3);
        } 

答案 2 :(得分:0)

错误在于:

**java.lang.IndexOutOfBoundsException: Index: 4, Size: 4**
 at java.util.ArrayList.RangeCheck(ArrayList.java:547)
 at java.util.ArrayList.get(ArrayList.java:322)
 at org.apache.jsp.admin.editproduct_jsp._jspService(editproduct_jsp.java:82)

并且说当arraylist只有4个元素时,它试图获得元素5(index = 4)。看来这个堆栈跟踪不是您在帖子上显示的代码的错误。我的猜测是,循环遍历“a1”ArrayList时出现错误,该列表只有4个元素并试图获得第五个元素。

答案 3 :(得分:-1)

while (rs.next()) {
        al = new ArrayList();

        al.add(rs.getString("product_id"));
        al.add(rs.getString("product_name"));
        al.add(rs.getString("product_description"));
        al.add(rs.getDouble("product_price"));
    }

您正在初始化循环内的arraylist。这也可能是一个问题,如果不是,可能会出现问题,如果这不是你所期望的。答案是由我猜的其他人给出的