Reputation: 2822
I have a JSP page where the user can select multiple check boxes (there are up to 10). Sometimes, if multiple check boxes are checked, NoSuchElement exception is thrown.
Servlet Code:
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();
}
}
}
I have not sure why it is throw, as I understood from the JavaDoc, it seems like happens if there is no element, but I thought iterateCurrentResultSet.hasNext() ensure that I was dealing with an element.
Upvotes: 0
Views: 93
Reputation: 10151
You check for iterateCurrentResultSet.hasNext()
in the outer loop, but if there is more than one locations
you call next()
more than once in the for-loop.
I would replace
String currentClassLocation = iterateCurrentResultSet.next().getSectionLocation();
by
if(!iterateCurrentResultSet.hasNext())
break; // exit for-loop if there is no next
String currentClassLocation = iterateCurrentResultSet.next().getSectionLocation();
Upvotes: 4