2 Replies Latest reply: May 14, 2008 9:41 PM by 807591 RSS

    Exception in thread "main" java.util.NoSuchElementException

    807591
      Hello there,
      I need to store resultset values in arrays.
      Following is my code:
      (Note : The function uses the jdbc connection with mysql that is return from other function.)
      public static void getResultSetInArray(Connection con) throws Exception
      {
      Map list = new HashMap();
      Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
      ResultSet rs = stmt.executeQuery("show columns from launchcomm.events");
      while(rs.next())
      {
      list.put(rs.getString("Field"),rs.getString("Type"));
      }
      rs.close();
      con.close();
      System.out.println("List = " + list + "\n");
      Iterator iter = null;
      iter = list.keySet().iterator();
      System.out.println("iter = " + iter);
      String field;
      String type;
      int i=1;
      System.out.println("list size = " + list.size());

      while(iter.hasNext() != false)
      {
      System.out.println("iter.hasNext() = " + iter.hasNext() +"......" +(i++));
      System.out.println("iter.next() = " + iter.next());
      field = (String)iter.next();
      type = (String)list.get(iter.next());
      }
      }
      ----
      Following is my output with error:
      List = {folder_name=varchar(50), is_repeat=tinyint(1), footer=text, thankyou_email=text, box_header_bg_color=varchar(25), organization_id=int(11), attendee_delete_right=tinyint(1), show_ticket_remaining=tinyint(1), order_confirmation_email=text, save_for_later=tinyint(1), start_register=tinyint(4), id=int(11), logo=varchar(100), publish=tinyint(1), end_unregister=datetime, owner_id=int(11), confirmation_email=text, audience_id=int(11), event_color_code=varchar(30), showcalendar=tinyint(1), registration_enddate=datetime, directory_name=varchar(20), eventstatus_id=int(11), contact_id=int(11), password_protect=tinyint(1), include_header=tinyint(1), thankyou_page=text, header=text, is_hotel_capacity=tinyint(1), want_close=tinyint(1), travel_housing_request=tinyint(1), box_header_text_color=varchar(25), default_location_id=int(11), end_reg_modification=datetime, user_id=int(11), passkey_eventcode=varchar(255), page_size=int(11), passkey_password=varchar(255), event_capacity=int(11), box_text_color=varchar(25), updated_on=datetime, link_color=varchar(25), ends_on=datetime, hotel_capacity=int(11), template_id=int(11), allow_overlap_session=tinyint(1), starts_on=datetime, reg_another_button=varchar(50), passkey_partnerid=int(11), personalized_url=tinyint(1), hide_start_date=tinyint(1), hide_end_date=tinyint(1), include_footer=tinyint(1), text_color=varchar(25), allow_another_registrant=tinyint(1), passkey_eventid=int(11), resize=tinyint(1), default_closetemplate=tinyint(1), dateformat=text, personalize_agenda=tinyint(1), cssfile_id=int(11), passkey_information=tinyint(1), confirmation_page=text, activate_waitlist=tinyint(1), box_border_color=varchar(25), google_analytics_code=text, show_iframe_design=tinyint(1), confirmation_mail_format=tinyint(1), url=varchar(100), bg_color=varchar(25), package_id=int(11), name=varchar(200), password=varchar(50), capacity=int(11), modify_registration=tinyint(1), is_event_capacity=tinyint(1), include_css=tinyint(1), passkey_username=varchar(255), created_on=datetime, promote_url=varchar(100), page_views=int(11), box_bg_color=varchar(25), title_text_color=varchar(25), registration_open_text=text, require_login=tinyint(1), closetemplate_description=text, registration_startdate=datetime, domain=varchar(200), timezone_id=varchar(100), description=text, tag=varchar(255), allow_unregister=tinyint(1), order_confirmation_page=text, css=text, showmap=tinyint(1)}

      iter = java.util.HashMap$KeyIterator@18a47e0
      list size = 95
      iter.hasNext() = true......1
      iter.next() = folder_name
      iter.hasNext() = true......2
      iter.next() = footer
      iter.hasNext() = true......3
      iter.next() = box_header_bg_color
      iter.hasNext() = true......4
      iter.next() = attendee_delete_right
      iter.hasNext() = true......5
      iter.next() = order_confirmation_email
      iter.hasNext() = true......6
      iter.next() = start_register
      iter.hasNext() = true......7
      iter.next() = logo
      iter.hasNext() = true......8
      iter.next() = end_unregister
      iter.hasNext() = true......9
      iter.next() = confirmation_email
      iter.hasNext() = true......10
      iter.next() = event_color_code
      iter.hasNext() = true......11
      iter.next() = registration_enddate
      iter.hasNext() = true......12
      iter.next() = eventstatus_id
      iter.hasNext() = true......13
      iter.next() = password_protect
      iter.hasNext() = true......14
      iter.next() = thankyou_page
      iter.hasNext() = true......15
      iter.next() = is_hotel_capacity
      iter.hasNext() = true......16
      iter.next() = travel_housing_request
      iter.hasNext() = true......17
      iter.next() = default_location_id
      iter.hasNext() = true......18
      iter.next() = user_id
      iter.hasNext() = true......19
      iter.next() = page_size
      iter.hasNext() = true......20
      iter.next() = event_capacity
      iter.hasNext() = true......21
      iter.next() = updated_on
      iter.hasNext() = true......22
      iter.next() = ends_on
      iter.hasNext() = true......23
      iter.next() = template_id
      iter.hasNext() = true......24
      iter.next() = starts_on
      iter.hasNext() = true......25
      iter.next() = passkey_partnerid
      iter.hasNext() = true......26
      iter.next() = hide_start_date
      iter.hasNext() = true......27
      iter.next() = include_footer
      iter.hasNext() = true......28
      iter.next() = allow_another_registrant
      iter.hasNext() = true......29
      iter.next() = resize
      iter.hasNext() = true......30
      iter.next() = dateformat
      iter.hasNext() = true......31
      iter.next() = cssfile_id
      iter.hasNext() = true......32
      iter.next() = confirmation_page
      iter.hasNext() = true......33
      iter.next() = box_border_color
      iter.hasNext() = true......34
      iter.next() = show_iframe_design
      iter.hasNext() = true......35
      iter.next() = url
      iter.hasNext() = true......36
      iter.next() = package_id
      iter.hasNext() = true......37
      iter.next() = password
      iter.hasNext() = true......38
      iter.next() = modify_registration
      iter.hasNext() = true......39
      iter.next() = include_css
      iter.hasNext() = true......40
      iter.next() = created_on
      iter.hasNext() = true......41
      iter.next() = page_views
      iter.hasNext() = true......42
      iter.next() = title_text_color
      iter.hasNext() = true......43
      iter.next() = require_login
      iter.hasNext() = true......44
      iter.next() = registration_startdate
      iter.hasNext() = true......45
      iter.next() = timezone_id
      iter.hasNext() = true......46
      iter.next() = tag
      iter.hasNext() = true......47
      iter.next() = order_confirmation_page
      iter.hasNext() = true......48
      iter.next() = showmap
      Exception in thread "main" java.util.NoSuchElementException
      at java.util.HashMap$HashIterator.nextEntry(HashMap.java:844)
      at java.util.HashMap$KeyIterator.next(HashMap.java:877)
      at GetDataTypeFromDB.getResultSetInArray(GetDataTypeFromDB.java:57)
      at GetDataTypeFromDB.main(GetDataTypeFromDB.java:16)

      ---
      I am unable to fine the reason for the exception.
      Please help me to solve my problem.
      Regards,
      Thevoice

      Edited by: TheVoice on May 14, 2008 12:01 AM
        • 1. Re: Exception in thread "main" java.util.NoSuchElementException
          807591
          I suggest putting the entire block of code in a try/catch/finally block where you close the connection in the finally block. In the catch block, put e.printStackTrace() so the exeception stack trace is sent to the console. From there, you should be able to see what the problem is. If not, pile on System.out.println() on each line of code to see what statement caused the exception to be thrown.
          • 2. Re: Exception in thread "main" java.util.NoSuchElementException
            807591
            TheVoice wrote:
            while(iter.hasNext() != false)
            {
            System.out.println("iter.hasNext() = " + iter.hasNext() +"......" +(i++));
            System.out.println("iter.next() = " + iter.next());
            field = (String)iter.next();
            type = (String)list.get(iter.next());
            }
            Your major problem is that iter.next() advances one place every time you call it, so in every iteration of that loop, you advance three steps. The first step is guaranteed to be okay, because you checked it with iter.hasNext(), but the other two are not. In addition, you are in effect "skipping" the printing of two out of every three items.

            To print out every item, you probably want to do something like this (call it only one and store it in a variable):
            while(iter.hasNext())
            {
                String field = (String)iter.next();
                System.out.println("iter.hasNext() = " + iter.hasNext() +"......" +(i++));
                System.out.println("iter.next() = " + field);
                String type = (String)list.get(field);
            }
            For additional clarity, I would recommend using Generics, Map.entrySet(), and the for-each loop:
            public static void getResultSetInArray(Connection con) throws Exception
            {
                Map<String,String> list = new HashMap<String,String>();
                Statement stmt = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,ResultSet.CONCUR_READ_ONLY);
                ResultSet rs = stmt.executeQuery("show columns from launchcomm.events");
                while(rs.next())
                {
                    list.put(rs.getString("Field"),rs.getString("Type"));
                }
                rs.close();
                con.close();
            
                for (Map.Entry<String,String> e : list.entrySet())
                {
                    String field = e.getKey();
                    String type = e.getValue();
                    // do stuff with them
                }
            }
            Edited by: spoon_ on May 14, 2008 7:34 PM