【问题标题】:Java MySQL IndexOutOfBounds ErrorJava MySQL IndexOutOfBounds 错误
【发布时间】:2012-09-14 07:10:42
【问题描述】:

为什么会出现这个错误:

java.lang.IndexOutOfBoundsException:索引:4,大小:4

我没有定义任何东西,并且在另一个运行良好的 servlet 上使用了相同的逻辑。在我的另一个 servlet 中,我选择了所有产品,所以我使用了两个数组列表:一个在另一个里面。我在这里尝试过,但仍然有同样的错误。我清楚地理解了这个错误,但我不知道如何用这种语法解决它。

谢谢。

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);
    } 
    %>

【问题讨论】:

  • 可能req/ 结尾,所以req.substring(req.lastIndexOf("/")+1) 会出错。
  • 你能发一个stacktrace吗?
  • 如果你不知道是哪一行抛出了令人费解的异常,它们通常会更清楚。因此,您不仅应该记录他的消息,而且至少应该记录最后一个堆栈跟踪条目的行。
  • a) 将您的代码从 jsp 移出到适当的 java 类中。 b)您向我们展示的代码块中未引发异常。可能有一些 jsp 标记,您正在尝试获取 al[4]。
  • 你可能是对的,张贴在下一个 jsp 页面上方采取这些东西

标签: java mysql jsp servlets


【解决方案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"));
    }

您正在循环内初始化数组列表。这也可能是一个问题,如果不是,如果这不是您所期望的,可能会产生问题。我猜是别人给的答案

【讨论】:

  • 为什么会是个问题?这是完全没有根据的,只会造成混乱。
  • 尝试将初始化移动到那里,仍然没有帮助
  • 这不是问题。这段代码会抛出 indexoutofbound.. 检查其他答案
  • @DoubleMalt:这就是使用“Might”这个词的原因。如果 OP 试图将数据放入单个数组列表中,那么这是一个错误,在这种情况下,因为它会生成很多数组列表。如果他/她每次循环运行时都需要不同的arrayLists,那么没问题....但是,我已经清楚地告诉了答案是别人给出的
  • @chaitanya10: 可以给用户提建议,改进代码。检查答案取决于用户,而不是我们。
【解决方案2】:

你可能在这一行遇到了这个异常

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

根据您的代码,这可能是唯一的情况。当您在 req 上调用 subString() 时,索引超出范围。最好的测试是将 sysout 的长度设置为 req

发布堆栈跟踪后进行编辑

把你的java代码改成这个 al = new ArrayList();
而(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);
} 

【讨论】:

  • 但同样的事情在另一个 servlet 中工作,两者都来自同一个 jsp 页面通过 javascript - function editRecord(product_id){ window.location.href="/Store/SelectProduct/"+产品ID; } function deleteRecord(product_id){ window.location.href="/Store/DeleteProduct/"+product_id; }
  • 你能发布堆栈跟踪吗
  • 正在这样做,但又遇到了错误,我将发布我的整个 servlet 并查看更清晰的新堆栈跟踪,我将在问题解决后将问题编辑为更整洁的版本,感谢您的努力顺便说一句,我非常感谢你和大家
  • 我猜代码差不多就是这样,所以发布更多没有区别,我会发布新的跟踪
  • @TrackmeifYouCan 哈哈,当然:)
【解决方案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);
        } 

【讨论】:

  • 谢谢,chaitanya10 的通知完全相同,我之前尝试使用 0 时告诉我它从 1 开始,所以我认为它总是 1,我现在需要阅读并学习。 . 谢谢
  • @TrackmeifYouCan 当然,np。 GL
【解决方案4】:

给出的错误是这样的:

**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(索引=4)。似乎此堆栈跟踪不是您在帖子中显示的代码的错误。我的猜测是,当循环遍历只有 4 个元素并试图获取第五个元素的“a1”ArrayList 时,稍后会出现错误。

【讨论】:

  • 有点奇怪,因为这个 servlet 只是进入编辑页面,没有其他活动
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-12-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-06-02
  • 1970-01-01
  • 2018-12-18
相关资源
最近更新 更多