0

I need to obtain all the keys present in my HashTable. I do that with the following code:

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...
}

}

IF request is made for report, then it below is code for report.

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;
}

// Only non-empty and non-null parameter are handed over to actual report generator method.

private void generateReport(Hashtable<String, String> param) {
    // TODO Auto-generated method stub
    Enumeration<String> keyset = param.keys();
    while (keyset.hasMoreElements()) {
        String e = keyset.nextElement();
    }
}

But I get a NoSuchElementException while getting the last element. My HashTable contains the keys {"Section", "Name", "Standard"} and in the while loop 'e' only get the value "Section" and "Standard". And the third time the exception is thrown.

Also i tried, [link] (Get keys from HashMap in Java) but same issue happens here.

Community
  • 1
  • 1
Kanishka Gupta
  • 197
  • 2
  • 3
  • 16
  • 2
    Can you produce a short but complete program demonstrating the problem? The code you've given works for me... – Jon Skeet Jun 23 '13 at 07:29
  • @JonSkeet I have added the code with little description of facts. Please have a look – Kanishka Gupta Jun 23 '13 at 07:43
  • 2
    Just a thought: You have a repeating pattern which would deserve a separate method Something like `private void addParam(Hashtable param, HttpServletRequest request, String paramName) { String v = request.getParameter(paramName); if ((v != null) && !v.isEmpty())) param.put(paramName, v); }` and then just call `addParam(param, request, "standard");` etc. – Petr Jun 23 '13 at 07:48
  • @PetrPudlák I will make this change, it will make code little more robust – Kanishka Gupta Jun 23 '13 at 07:53
  • @KanishkaGupta: That's not a short but complete program. You should be able to demonstrate the problem without using servlets at all, in a little console app. Does your `generateReport` method *really* do nothing in the loop, but still fail? – Jon Skeet Jun 23 '13 at 07:54
  • @JonSkeet I tried `generateReport` method in console app, and i got the correct output. but similar stuff is not working in Servlet, may be root cause of exception is something else, that is why i posted whole code. – Kanishka Gupta Jun 23 '13 at 08:14
  • @KanishkaGupta: If that's really the *whole* code then you should just remove your `generateReport` call, as it doesn't actually do anything useful. – Jon Skeet Jun 23 '13 at 08:20

0 Answers0