0

我有一个 JSP 页面,用户可以在其中选择多个复选框(最多 10 个)。有时,如果选中多个复选框,则会引发 NoSuchElement 异常。

小服务程序代码:

 Iterator<ClassInfo> iterateCurrentResultSet = resultSet.iterator();

        //If the current class's location isn't contained in "locations", the user didn't select it so remove
        //It from the results.
        while(iterateCurrentResultSet.hasNext())
        {
            //Evaluate every location they checked for, remove any class that isn't one of these locations.
            for(String selectedLocation : locations)
            {
                //IF THE EXCEPTION OCCURS, IT DOES HERE. Current class location, it this doesn't match the user's selected criteria, it's removed.
                String currentClassLocation = iterateCurrentResultSet.next().getSectionLocation();

                //Check if the user wants to see other classes, if not, continue on.
                if(selectedLocation.equals("OTH"))
                {
                    //If this is false, keep it because it is an "Other" class, such as SFD, CWD, etc.
                    if(mainCampusCode.contains(currentClassLocation))
                    {
                        iterateCurrentResultSet.remove();
                    }//doNothing();
                }
                else
                {
                    //If it does not have one of the selected locations, remove it.
                    if(!currentClassLocation.contains(selectedLocation))
                    {
                        iterateCurrentResultSet.remove();
                    }//doNothing();
                }

            }
        }

我不确定它为什么会抛出,正如我从 JavaDoc 中了解到的那样,如果没有元素,它似乎会发生,但我认为 iterateCurrentResultSet.hasNext() 确保我正在处理一个元素。

4

1 回答 1

4

您在外部循环中检查 for iterateCurrentResultSet.hasNext(),但如果有多个,则在 for 循环中多次locations调用next()

我会替换

String currentClassLocation = iterateCurrentResultSet.next().getSectionLocation();

经过

if(!iterateCurrentResultSet.hasNext())
  break; // exit for-loop if there is no next
String currentClassLocation = iterateCurrentResultSet.next().getSectionLocation();
于 2013-01-28T21:54:15.703 回答