Results 1 to 8 of 8
  1. #1
    asa
    asa is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default exception when trying to add class in a hashset

    Hello,

    I am trying to extract some values from a DB table and save it in a hashset.

    Here is my code,

    Java Code:
    public HashSet selectAll() {
    
    		HashSet users=new HashSet ();
    		ResultSet rs=null;
    		boolean b = false;
    		try {
    		Statement st = con.createStatement();
    		rs = st.executeQuery("select id,name,age,job from company.user;");
    		
    		while(rs.next())
    		{
    			b= users.add(new UserModel(rs.getInt("id"), rs.getInt("age"), rs.getString("name"), rs.getString("job")));	
    			System.out.println(b);
    		}
    				
    		}
    		catch (SQLException ex) {
    		ex.printStackTrace();
    		}
    		
    		return users;
    	}
    when I try executing the code, I get this exception and don't understand it.

    true
    Exception in thread "AWT-EventQueue-0" java.lang.ClassCastException: Assignment1.UserModel cannot be cast to java.lang.String
    at Assignment1.Displayer.<init>(Displayer.java:29)
    at Assignment1.MainFrame$4.actionPerformed(MainFrame. java:66)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:2028)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2351)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6373)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3267)
    at java.awt.Component.processEvent(Component.java:613 8)
    at java.awt.Container.processEvent(Container.java:208 5)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4735)
    true
    true
    true
    true
    true
    true
    true
    at java.awt.Container.dispatchEventImpl(Container.jav a:2143)
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4621)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:4282)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:4212)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2129)
    at java.awt.Window.dispatchEventImpl(Window.java:2478 )
    at java.awt.Component.dispatchEvent(Component.java:45 65)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.j ava:679)
    at java.awt.EventQueue.access$000(EventQueue.java:85)
    at java.awt.EventQueue$1.run(EventQueue.java:638)
    at java.awt.EventQueue$1.run(EventQueue.java:636)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:98)
    at java.awt.EventQueue$2.run(EventQueue.java:652)
    at java.awt.EventQueue$2.run(EventQueue.java:650)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.AccessControlContext$1.doIntersectio nPrivilege(AccessControlContext.java:87)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 649)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:296)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:211)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:196)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:188)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:122)


    Thanks.
    Last edited by Fubarable; 05-19-2012 at 10:49 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: exception when trying to add class in a hashset

    Which line causes this exception?

    Edit: code tags added to your question.

  3. #3
    asa
    asa is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: exception when trying to add class in a hashset

    It looks this line
    Java Code:
    b= users.add(new UserModel(rs.getInt("id"), rs.getInt("age"), rs.getString("name"), rs.getString("job")));
    even I see true printed for the first iteration!

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: exception when trying to add class in a hashset

    And you're sure that you're not using a generic HashSet<String> anywhere?

  5. #5
    asa
    asa is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: exception when trying to add class in a hashset

    Yes, I call it from here.


    Java Code:
    HashSet rs = DB.selectAll();
    		Iterator iterator = rs.iterator();
    		
    		JComboBox names = new JComboBox();
    		while (iterator.hasNext()) {
    			names.addItem(iterator.next());
    		}
    		this.add(names);

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: exception when trying to add class in a hashset

    I can't see how that line will cause that error.

    Are you 100% sure that that is line 29 from the Displayer class?

  7. #7
    asa
    asa is offline Member
    Join Date
    May 2012
    Posts
    11
    Rep Power
    0

    Default Re: exception when trying to add class in a hashset

    No, it is in different class.
    I got confused becuse it printed the first line then the exception appeared.

    Thanks.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: exception when trying to add class in a hashset

    Quote Originally Posted by asa View Post
    No, it is in different class.
    I got confused becuse it printed the first line then the exception appeared.
    Then have you solved your problem? Sorry, but this is very confusing to me.
    Last edited by Fubarable; 05-20-2012 at 01:55 AM.

Similar Threads

  1. Class not found exception for the servlet class.
    By mazem in forum Java Servlet
    Replies: 2
    Last Post: 08-24-2011, 09:20 AM
  2. HashSet
    By Dayanand in forum New To Java
    Replies: 7
    Last Post: 03-12-2011, 09:37 AM
  3. Getting value for key in HashSet
    By Venny in forum New To Java
    Replies: 12
    Last Post: 02-02-2011, 08:42 AM
  4. :( anyone here plz help on HashSet
    By waklo99 in forum New To Java
    Replies: 8
    Last Post: 09-20-2010, 03:02 AM
  5. Replies: 1
    Last Post: 01-03-2010, 09:48 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •