Results 1 to 8 of 8
- 05-19-2012, 10:15 PM #1
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
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,
when I try executing the code, I get this exception and don't understand it.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; }
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.
-
Re: exception when trying to add class in a hashset
Which line causes this exception?
Edit: code tags added to your question.
- 05-19-2012, 11:00 PM #3
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
Re: exception when trying to add class in a hashset
It looks this line
even I see true printed for the first iteration!Java Code:b= users.add(new UserModel(rs.getInt("id"), rs.getInt("age"), rs.getString("name"), rs.getString("job")));
-
Re: exception when trying to add class in a hashset
And you're sure that you're not using a generic HashSet<String> anywhere?
- 05-19-2012, 11:41 PM #5
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
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);
-
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?
- 05-20-2012, 12:31 AM #7
Member
- Join Date
- May 2012
- Posts
- 8
- Rep Power
- 0
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.
-
Re: exception when trying to add class in a hashset
Last edited by Fubarable; 05-20-2012 at 01:55 AM.
Similar Threads
-
Class not found exception for the servlet class.
By mazem in forum Java ServletReplies: 2Last Post: 08-24-2011, 09:20 AM -
HashSet
By Dayanand in forum New To JavaReplies: 7Last Post: 03-12-2011, 09:37 AM -
Getting value for key in HashSet
By Venny in forum New To JavaReplies: 12Last Post: 02-02-2011, 08:42 AM -
:( anyone here plz help on HashSet
By waklo99 in forum New To JavaReplies: 8Last Post: 09-20-2010, 03:02 AM -
Exception Class for class that compares objects variables. Help Please.
By darkblue24 in forum New To JavaReplies: 1Last Post: 01-03-2010, 09:48 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks