?
Solved

Error message when display values in HashMap

Posted on 2004-10-29
3
Medium Priority
?
414 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
Comment
Question by:jen2483
  • 2
3 Comments
 
LVL 13

Expert Comment

by:petmagdy
ID: 12446997
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
ID: 12447018
>>not like this

sorry i mean like this

....
0
 
LVL 3

Accepted Solution

by:
Gunt earned 1000 total points
ID: 12449596
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

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The following information will get you familiar with your new DV server, including the (mt) Account Center, the Plesk Control Panel, our world-renowned support department and the rest of the (mt) tools that come with your new service.
There are a few different ways to preview your site before DNS resolves it to your (mt) Media Temple server.  The Plesk platform makes it easy.  See the following guide to learn how.
Planning to migrate your EDB file(s) to a new or an existing Outlook PST file? This video will guide you how to convert EDB file(s) to PST. Besides this, it also describes, how one can easily search any item(s) from multiple folders or mailboxes…
There may be issues when you are trying to access Outlook or send & receive emails or due to Outlook crash which leads to corrupt or damaged PST file. To eliminate the corruption from your PST file, you need to repair the corrupt Outlook PST file. U…
Suggested Courses

616 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