【问题标题】:Array Index out of bound in jsp filejsp文件中的数组索引超出范围
【发布时间】:2014-09-04 06:25:50
【问题描述】:

js文件:

为什么 dropdown1 的大小是 1?

function savethechanges(){
for(var i=0;i<count;i++)
{
    dropdown1[i]=document.getElementById("sel"+i).value;
    kot[i]=document.getElementById("kot"+i).value;
    item[i]=document.getElementById("itemcode"+i).value;
    if(dropdown1[i] == 0){
        document.detailsview.action="BillCB.jsp?method=" + "11" + "&itemcode=" +item[i]+ "&kot=" +kot[i]+ "&itemStatus1=" +dropdown1[i]+ "&billno=" +billno;
    }
    else if(dropdown1[i] == 1){
        document.detailsview.action="BillCB.jsp?method="+"9"+"&itemcode="+item[i]+"&kot="+kot[i]+"&itemStatus1="+dropdown1[i]+ "&billno="+billno;
    }
    else{
        document.detailsview.action= "BillCB.jsp?method="+"10"+"&itemcode="+item[i]+"&kot="+kot[i]+"&itemStatus1="+dropdown1[i]+ "&billno="+billno;
    }
}
}

JSP 文件:

在此处检索时,dropdown1 长度为 1。但对于 kot 和项目长度为 18 请提供您的解决方案!

 case 11:
 gotMethod = true;
 billdetails_be.billno = Integer.valueOf(request.getParameter("billno"));
 String[] kotCB2=request.getParameterValues("kot");
 String[] itemCB2=request.getParameterValues("itemcode");
 String[] statCB2=request.getParameterValues("itemStatus1");
 int[] kotarr2=new int[kotCB2.length];
 int[] itemarr2=new int[itemCB2.length];
 int[] statarr2=new int[statCB2.length];
 System.out.println("IN AVAILABLE:length of array is:"+statCB2.length);
 System.out.println("IN AVAILABLE:length of array is:"+kotCB2.length);
 for(int i=1;i<itemarr2.length;i++)
 {   
     kotarr2[i]=Integer.parseInt(kotCB2[i]);
 }
 for(int i=1;i<itemarr2.length;i++)
 {  
     itemarr2[i]=Integer.parseInt(itemCB2[i]);
 }
 for(int i=1;i<itemarr2.length;i++)
 { 
     statarr2[i]=Integer.parseInt(statCB2[i]);  
 }
 for(int i=1;i<itemarr2.length;i++)
 {
     int kotint2=kotarr2[i];
     int itemint2=itemarr2[i];
     int statint2=statarr2[i];
     System.out.println( i+"the value of kot in available"+ kotint2);
     int availablebill = websrv.availablebill(billdetails_be.billno, kotint2,   itemint2,statint2); 
  }

设计页面(jsp)

<%
for(int i=0;i<count;i++){
%>
<TR>
<TD>  <input id="itemcode<%=i%>" type="hidden" name="itemcode" value=" <%=billdetails_be.get(i).itemcode%>"></TD>
<TD><%=billdetails_be.get(i).itemdescription%></TD>
<TD>&nbsp;&nbsp;<input id="quantity<%=i%>" type="text" name="quantity"  style="width:  30px;" readOnly="readonly" value="<%=billdetails_be.get(i).quantity%>" >
<input type="submit"  id="inc<%=i%>" onclick= "doIt1(1,<%=i%>);"  value="+" style="width:20px; height:20px; border-radius:10px; padding:0 0; " /> 
<input type="submit"  id="dec<%=i%>" onclick="doIt1(-1,<%=i%>);"   value="-" style="width:20px; height:20px; border-radius:10px; padding:0 0; "/>
</TD>
<TD>&nbsp;&nbsp;<%=billdetails_be.get(i).price%></TD>
<TD>&nbsp;&nbsp;<%=billdetails_be.get(i).itemstatusdescription%></TD>
<td>
<select name="statusi" id="sel<%=i%>">
<option value="0">Available</option>
<option value="1">Unavailable</option>
<option value="2">Delete</option>
</select> 
</td>               
<td> <input id ="kot<%=i%>" type="text" style="border: 0px solid #000000;" name="kot"   style="cursor: text" readonly="readonly" value="<%=billdetails_be.get(i).kotno%>"></td>         
<TD> <input id="myquantity<%=i%>" type="hidden" name="quantity1" value="     <%=billdetails_be.get(i).quantity%>"></TD>
</TR> 
<%
} 
%>
<td><input style="width: 150px; " class="btn btn-green btn-large" type="submit" name="save" id="save" value="save"  class="button" onclick="savetheChanges(<%=count%>);">    </td>

新变化

1.this 在 js 文件中,我从中将值传递给 BillCB.jsp

 document.detailsview.action="BillCB.jsp?method=" + "11" + "&itemcode=" +itemcode1[i]+ "&kot=" +kot1[i]+ "&itemStatus1=" +selection[i]+ "&billno=" +billno;

2.this 在 BillCB.jsp 文件中(方法:11)

 String[] statCB2=request.getParameterValues("itemStatus1");
 int[] statarr2=new int[statCB2.length];
 System.out.println("IN AVAILABLE:length of STATUS array is:"+statCB2.length);

在 AVAILABLE 中:STATUS 数组的长度为:1-这是问题所在。

【问题讨论】:

  • 请提供更多信息,例如 ID 为“sel”+i、“kot”+i 和“itemcode”+i 的元素的实际标记……不清楚这些元素的来源从

标签: java javascript arrays jsp


【解决方案1】:
  1. 首先,有点奇怪的是,在您的所有循环中,您都在循环通过 itemarr2,所以我不明白为什么您没有一个包含所有逻辑的循环。
  2. 其次,您循环遍历 itemarr2,然后使用 itemarr2 的当前索引来访问和分配其他数组中的值。如果其中任何一个数组的大小小于 itemarr2,您将收到数组 indexOutofbound 异常。

    for(int i=1;i<itemarr2.length;i++)
    {   
       kotarr2[i]=Integer.parseInt(kotCB2[i]); //If size of itemarr2 is bigger than kotarr2 or is bigger than kotCB2. You will get an array indexOutofbound exception.
    }
    for(int i=1;i<itemarr2.length;i++)
    {  
       itemarr2[i]=Integer.parseInt(itemCB2[i]); //If size of itemarr2 is bigger than itemCB2. You will get an array indexOutofbound exception.
    }
    for(int i=1;i<itemarr2.length;i++)
    { 
       statarr2[i]=Integer.parseInt(statCB2[i]);  //If size of itemarr2 is bigger than statarr2 or is bigger than statCB2. You will get an array indexOutofbound exception.  
    }
    for(int i=1;i<itemarr2.length;i++)
    {
      int kotint2=kotarr2[i];  //If size of itemarr2 is bigger than kotarr2. You will get an array indexOutofbound exception.
      int itemint2=itemarr2[i]; //If size of itemarr2 is bigger than itemarr2. You will get an array indexOutofbound exception.
      int statint2=statarr2[i]; //If size of itemarr2 is bigger than statarr2. You will get an array indexOutofbound exception.
      System.out.println( i+"the value of kot in available"+ kotint2);
      int availablebill = websrv.availablebill(billdetails_be.billno, kotint2, itemint2,statint2); 
    }
    

因此,您必须确保 kotarr2、kotCB2、itemCB2、statarr2、statCB2 的大小始终大于或等于 itemarr2 的大小,并且正如我所说,您可以将所有逻辑放入单个循环中。

关于您的 Js 文件,我们需要查看您如何设置计数,但如果您的计数值大于下拉列表的大小1,那么您将得到一个数组 indexOutofbound 异常。

更新:

您说 count = 18,但您没有使用 count 初始化 statarr2。您使用 statCB2.length 对其进行初始化。

     int[] statarr2=new int[statCB2.length];

反过来 statCB2.length 也没有使用 count 进行初始化,而是使用 request.getParameterValues("itemStatus1"); 进行初始化;

      String[] statCB2=request.getParameterValues("itemStatus1");

所以我相信您的项目状态等于 1。特别是因为在您的 Js 上您有以下代码。

   ... "&itemStatus1=" +dropdown1[i]+ ...

所以 itemStatus1 将等于 dropdown1[i] 上的项目。因此 itemStatus1 不等于 count 或 dropdown1 的大小。它仅等于索引 (i) 处下拉列表 1 中的项目数,我认为它是 1;您可以随时使用 alert() 或 console.log() 进行测试。

更新:

您说 count = 18,但您没有使用 count 初始化 statarr2。您使用 statCB2.length 对其进行初始化。

     int[] statarr2=new int[statCB2.length];

反过来 statCB2.length 也没有使用 count 进行初始化,而是使用 request.getParameterValues("itemStatus1"); 进行初始化;

      String[] statCB2=request.getParameterValues("itemStatus1");

所以我相信您的项目状态等于 1。特别是因为在您的 Js 上您有以下代码。

   ... "&itemStatus1=" +dropdown1[i]+ ...

所以 itemStatus1 将等于 dropdown1[i] 上的项目。因此 itemStatus1 不等于 count 或 dropdown1 的大小。它仅等于索引 (i) 处下拉列表 1 中的项目数,我认为它是 1;您可以随时使用 alert() 或 console.log() 进行测试。


如果您希望 itemStatus1 等于 dropdown1 的大小,然后插入

     ... "&itemStatus1=" +dropdown1[1]+ ...  

你应该使用

     ... "&itemStatus1=" +dropdown1.length+ ...  or 

     ... "&itemStatus1=" +dropdown1.size+ ...   (whichever is correct).

另外另一种选择不是使用 itemStatus1 而是使用 count 或 dropdown1.length 初始化您的 statCB2 数组

String[] statCB2= new String[count] or  String[] statCB2= new String[dropdown1.length] 

这应该可以解决问题

【讨论】:

  • 我同意 itemarr 的大小应该更大。但问题是 statarr2 的大小是 1。它应该是 18。
  • 我还不清楚。你能告诉我在哪里进行更改吗?
  • 但这里的概念是我使用for循环(@jsfile)传递下拉列表1的值。根据值的数量,这里(jsp文件)应该创建字符串数组的大小。请检查我如何检索 kot 和项目代码。
  • 问题是我无法调试你的代码。为了帮助你,我需要知道 jsp 文件中的哪一行给你错误,还有 itemStatus1 的大小/长度和值
  • 看到问题是你假设 kotarr2、kotCB2、itemarr2、itemCB2、statarr2、statCB2、itemStatus1 和 dropdown1 都具有相同的大小。如果它们中的任何一个更小,它会给你带来麻烦。我认为您不应该在所有循环中使用 itemarr2.length 作为控制值。尝试使用相同大小的数组。所以对于 kotarr2 尝试使用 kotCB2.lenght 循环。对于itemarr2 尝试使用itemCB2.length 循环;对于 statarr2 尝试使用 statCB2.lenght 循环。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多