participate


Java Desktop Applications - NullPointerException using JComboBox.removeAllItems()
<<   Back to Forum  |   Give us Feedback
This topic has 4 replies on 1 page.
Strummin4Christ
Posts:4
Registered: 7/20/04
NullPointerException using JComboBox.removeAllItems()   
Jul 29, 2004 10:53 AM

 
I have created an application that uses two comboboxes, one for the states and one for the cities. The cities will be populated after the state has been chosen. It all works fine until I try to go back and choose another state. When I pick another state a NullPointerException is thrown at the call to JComboBox.removeAllItems(). Can someone enlighten me to the problem and/or a possible workaround? Thanks! -Eric
 
icewalker2g
Posts:1,156
Registered: 4/16/04
Re: NullPointerException using JComboBox.removeAllItems()   
Jul 30, 2004 3:07 AM (reply 1 of 4)  (In reply to original post )

 
Just replace the current model for the state instead of removing the items and repopulating the model.
box.setModel( new DefaultComboBoxModel(cityVector) ) 
This will remove the information for the old cities and replace it with a new one

Good luck

ICE
 
Strummin4Christ
Posts:4
Registered: 7/20/04
Re: NullPointerException using JComboBox.removeAllItems()   
Aug 5, 2004 8:57 AM (reply 2 of 4)  (In reply to #1 )

 
Thanks a bunch. That solved the problem!! It seems so obvious now, haha. Thanks again!

Eric
 
cuculi54986@yahoo.com
Posts:2
Registered: 4/29/04
Re: NullPointerException using JComboBox.removeAllItems()   
Feb 1, 2006 9:49 AM (reply 3 of 4)  (In reply to #1 )

 
Just wanted to say thanks for the post. I was having the same issue!
 
johncurtisgunther
Posts:1
Registered: 6/13/06
Re: NullPointerException using JComboBox.removeAllItems()   
Jun 13, 2006 8:09 AM (reply 4 of 4)  (In reply to #1 )

 
This solved my problem too, and I am using JDK 1.5 (so it's not just an older JDK issue).

Incidentally, some other forums indicated that this occurs only when the JComboBox getItemCount returns 0 and you try to issue removeAllItems(). However, guarding against a zero item count didn't fix my problem, whereas this post's "setModel" approach did.

Also using invokeLater() to set up the GUI didn't fix it either, something I initially though was it because the problem went away if I sat on the same line in the debugger for 5 seconds or so before proceeding forward.

Thanks--your post was very helpful to me.

(Is this deleteAllItems() behaviour considered to be bug in the JDK?)

John
 
This topic has 4 replies on 1 page.
Back to Forum
 
Read the Developer Forums Code of Conduct

Click to email this message Email this Topic

Edit this Topic
  
 
 
Forums Statistics
    Users Online : 26
  • Guests : 129

About Sun forums
  • Sun Forums is a large collection of user generated discussions. It is here to help you ask questions, find answers, and participate in discussions.

    Check out our guide on Getting started with Sun Forums for a full walkthrough of how to best leverage the benefits of this community.

Powered by Jive Forums