java.util.NoSuchElementException:哈希表枚举器
我需要获取我的java.util.NoSuchElementException:哈希表枚举器,java,collections,nosuchelementexception,Java,Collections,Nosuchelementexception,我需要获取我的哈希表中的所有密钥。我使用以下代码执行此操作: protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // Important: doGet get the 4 input. // Parameter name: "Standard", "Section", "Name", "Rollno"
哈希表中的所有密钥。我使用以下代码执行此操作:
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
// Important: doGet get the 4 input.
// Parameter name: "Standard", "Section", "Name", "Rollno"
// But i am dealing with only 3 input to test the code. (not considering "Rollno" as of now, So RollNo will be empty string as of now.)
String output = "";
response.setContentType("text/html");
// referer tag in the header tells that which page sent the request.
// If this request is sent by Search.jsp page, the look at which button
// made the request and process the request accordingly.
if (request.getHeader("referer") != null
&& (request.getHeader("referer").endsWith("Search.jsp"))) {
Enumeration<String> parma_names = request.getParameterNames();
while ((parma_names != null) && (parma_names.hasMoreElements())) {
String ele_name = parma_names.nextElement();
if (ele_name.equalsIgnoreCase("data1")) {
output = requestMadeForDialog(request, ele_name);
response.getWriter().write(output);
} else {
// See here...
output = requestMadeForReport(request, ele_name);
response.getWriter().write(output);
break;
}
}
} else {
// something here...
}
protectedvoiddoget(HttpServletRequest),
HttpServletResponse响应)引发ServletException,IOException{
//重要提示:doGet获取4个输入。
//参数名称:“标准”、“节”、“名称”、“卷号”
//但我只处理3个输入来测试代码(目前不考虑“Rollno”,所以Rollno将是空字符串)
字符串输出=”;
response.setContentType(“text/html”);
//标头中的referer标记指示发送请求的页面。
//如果此请求是通过Search.jsp页面发送的,则“查看哪个”按钮
//提出请求并相应地处理请求。
if(request.getHeader(“referer”)!=null
&&(request.getHeader(“referer”).endsWith(“Search.jsp”)){
枚举参数名称=request.getParameterNames();
而((parma_names!=null)&&(parma_names.hasMoreElements()){
字符串ele_name=parma_name.nextElement();
if(ele_name.equalsIgnoreCase(“数据1”)){
输出=requestMadeForDialog(请求,元素名称);
response.getWriter().write(输出);
}否则{
//看这里。。。
输出=requestMadeForReport(请求,元素名称);
response.getWriter().write(输出);
打破
}
}
}否则{
//这里有些东西。。。
}
}
若要求报告,则以下为报告代码
private String requestMadeForReport(HttpServletRequest request,
String ele_name) {
log("requestMadeForReport and element name is " + ele_name);
// received parameter may be empty, If param is empty then it should not
// be included in where clause.
String standard = request.getParameter("standard");
String section = request.getParameter("section");
String name = request.getParameter("name");
String rollno = request.getParameter("rollno");
Hashtable<String, String> param = new Hashtable<>();
if (standard != null && (!standard.isEmpty())) {
param.put("standard", standard);
}
if (section != null && (!section.isEmpty())) {
param.put("section", section);
}
if (name != null && (!name.isEmpty())) {
param.put("name", name);
}
if (rollno != null && (!rollno.isEmpty())) {
param.put("rollno", rollno);
}
generateReport(param);
return null;
}
private String requestMadeForReport(HttpServletRequest请求,
字符串元素(名称){
日志(“requestMadeForReport,元素名称为”+ele_name);
//接收的参数可能为空,如果参数为空,则不应为空
//包含在where子句中。
字符串标准=request.getParameter(“标准”);
String section=request.getParameter(“section”);
字符串名称=request.getParameter(“名称”);
字符串rollno=request.getParameter(“rollno”);
Hashtable param=新的Hashtable();
if(standard!=null&(!standard.isEmpty()){
参数put(“标准”,标准);
}
if(section!=null&(!section.isEmpty()){
参数put(“节”,节);
}
如果(name!=null&(!name.isEmpty()){
参数put(“名称”,名称);
}
if(rollno!=null&(!rollno.isEmpty()){
参数put(“rollno”,rollno);
}
发电机报告(参数);
返回null;
}
//只有非空和非空的参数才移交给实际的报表生成器方法
private void generateReport(Hashtable<String, String> param) {
// TODO Auto-generated method stub
Enumeration<String> keyset = param.keys();
while (keyset.hasMoreElements()) {
String e = keyset.nextElement();
}
}
private void generateReport(哈希表参数){
//TODO自动生成的方法存根
枚举键集=param.keys();
while(keyset.hasMoreElements()){
字符串e=keyset.nextElement();
}
}
但是在获取最后一个元素时,我得到了一个NoTouchElementException
。
我的哈希表
包含键{“Section”、“Name”、“Standard”}
而在while循环中,“e”仅获取值“Section”和“Standard”。
第三次抛出异常
我也尝试过,[link](),但同样的问题也发生在这里。你能制作一个简短但完整的程序来演示这个问题吗?你给我的代码对我有用…@JonSkeet我添加的代码几乎没有事实描述。请看一看只是想一想:您有一个重复的模式,它应该有一个单独的方法,比如private void addParam(Hashtable param,HttpServletRequest,String paramName){String v=request.getParameter(paramName);if((v!=null)和&!v.isEmpty())param.put(paramName,v);}
然后只需调用addParam(param,request,“standard”)代码>等等@PetrPudlák我会做这个改变,它会让代码变得更多robust@KanishkaGupta:这不是一个简短但完整的程序。您应该能够在一个小型控制台应用程序中演示这个问题,而无需使用servlet。您的generateReport
方法是否真的在循环中什么都不做,但仍然失败?