【发布时间】:2018-07-31 13:59:30
【问题描述】:
我的代码增加了arraylist.get(index) 中的索引,当我到达列表末尾时,它被用作下一个按钮,它应该抛出IndexOutOFBoundsException 并在文本框中显示一条消息。问题是,如果我不断增加异常不会被捕获并且它会不断重启arraylist,就像它走到尽头然后从头开始一样。
搜索索引在按钮处理程序之外初始化为 0
try{
setResidentialFields(results,searchindex);
}
catch(ArrayIndexOutOfBoundsException e){
jTextField17.setText("NO MORE PROPERTIES");
}
searchindex++; //increment for next element in current search
这是setResidentialFields 方法中的一行
jTextField17.setText(String.valueOf(r.get(index).getTax())); //tax
要回复第一个答案,这就是我更改代码的方式
searchindex++;
try{
jButton1.doClick();
}
catch(IndexOutOfBoundsException e){
jTextField7.setText("No more properties to display");
}
还是不行
【问题讨论】:
-
我想补充一点,当我在 setResidentialFields(results,3343); 行中手动输入超出范围的值时程序崩溃但出现异常但不是当我在 buttonclick 上增加变量时
-
你确定你捕捉到了正确的异常吗? docs.oracle.com/javase/8/docs/api/java/util/… 说
IndexOutOfBoundsException -
你确定
setResidentialFields正在抛出 ArrayIndexOutOfBoundsException 吗? -
当我用 333 替换 'searchindex' 时它会抛出 IndexOutOfBoundsException,但当 searchindex 变量超出范围时不会在示例中我尝试 arrayindexoutofbounds 只是为了调试
-
我也尝试过 IndexOutOfBoundsException。不起作用,它只是在到达结束后不断重新启动显示从 0 开始的元素的数组列表
标签: java exception exception-handling nullpointerexception