Solved

Error message when display values in HashMap

Posted on 2004-10-29
396 Views
Last Modified: 2011-09-20
How do I display the values in HashMap in JSP page? I use the following code, I can get HashMap and display the value in it, but I got error messages. Please help

LabelSession label = null;      
 HashMap labelHash= null;
try{                                            
   int id = 1002478;
   Context context = UtilHelper.getInitialContext();
   Object object =context.lookup("ejb/com/sbc/nd/ejb/traffic_report/LabelSessionHome");
   LabelSessionHome home = (LabelSessionHome)PortableRemoteObject.narrow(object, LabelSessionHome.class);
  label = home.create();                                        
  labelHash = label.getLabel(id);                                      
  System.out.println("labelHash::"+labelHash);
                                                             
  Set keySet = labelHash.keySet();
  System.out.println("keySet :"+keySet );
  Iterator conItor = labelHash.values().iterator();
  System.out.println("conItor :"+conItor );
  Iterator keyItor = keySet.iterator();
  System.out.println("keyItor :"+keyItor );
  while( keyItor.hasNext()){
                                    String content = (String)conItor.next();                            
                                     System.out.println("content: "+content);
                            %>      
                              <option ><%=content %></option>
                            <%   }
                               }catch (Exception exception){
                                 out.println("Exception occured ... " + exception.getMessage());
                                 exception.printStackTrace();
                               }
                             %>                                    
                           </select>      

[10/28/04 19:32:37:325 PDT] 2a6e10cf SystemErr     R java.util.NoSuchElementException
[10/28/04 19:32:37:325 PDT] 2a6e10cf SystemErr     R at java.util.HashMap$HashIterator.nextEntry(HashMap.java(Compiled Code))
[10/28/04 19:32:37:325 PDT] 2a6e10cf SystemErr     R at java.util.HashMap$ValueIterator.next(HashMap.java(Compiled Code))
                 
0
Question by:jen2483
    3 Comments
     
    LVL 13

    Expert Comment

    by:petmagdy
    not like this

     while( keyItor.hasNext()){
                                        String key= (String)keyItor.next();          
                                        String content = labelHash.get(key);
                                         System.out.println("content: "+content);
                                %>    
                                  <option ><%=content %></option>
                                <%   }
                                   }catch (Exception exception){
                                     out.println("Exception occured ... " + exception.getMessage());
                                     exception.printStackTrace();
                                   }
                                 %>        
    0
     
    LVL 13

    Expert Comment

    by:petmagdy
    >>not like this

    sorry i mean like this

    ....
    0
     
    LVL 3

    Accepted Solution

    by:
    If you need to get both key and value during the iteration, the best method is to iterate the entrySet:

    for (Iterator iter = map.entrySet().iterator(); iter.hasNext() ; ) {
      Map.Entry entry = (Map.Entry) iter.next();
      System.out.println("Key is: " + entry.getKey());
      System.out.println("Value is: " + entry.getValue());
    }
    0

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone. Privacy Policy Terms of Use

    Featured Post

    Top 6 Sources for Identifying Threat Actor TTPs

    Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

    Suggested Solutions

    Resolve Outlook connectivity issues after moving mailbox to new Exchange 2016 server
    Data center, now-a-days, is referred as the home of all the advanced technologies. In-fact, most of the businesses are now establishing their entire organizational structure around the IT capabilities.
    With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
    This video Micro Tutorial is the second in a two-part series that shows how to create and use custom scanning profiles in Nuance's PaperPort 14.5 (http://www.experts-exchange.com/articles/17490/). But the ability to create custom scanning profiles a…

    877 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    15 Experts available now in Live!

    Get 1:1 Help Now