Removing all Items from a combo box in Java
Asked Answered
M

9

8

I need to remove all items from the combo box

int itemCount = combo.getItemCount();

for(int i = 0; i < itemCount; i++){
  combo.removeItemAt(0);
}

This code will remove all items except the last one. It gives a NullPointerException. How to fix that?

Menashem answered 3/9, 2012 at 13:59 Comment(5)
While I agree with the answers to use the built-in method to do this, this code works just fine when I run it myself. Are you sure your problem isn't elsewhere?Dmso
yeah, I used the removeAllItems() method. It removed all items. But still gives the exception.Menashem
You might have a threading issue. Are there other Threads accessing the combobox (and editing it)?Mechanist
ResultSet result1= null; //removing existing items before adding combo.removeAllItems(); result1 = DBOptions.executeSQLQuery(query); try { while(result1.next()){ String data = result1.getString(1); combo.addItem(data); }Menashem
@chathura2020 Add the code to your question, not to a comment...Foramen
M
38

The code in the question would normally work. However, it looks like a threading issue. Another thread may be messing with the items.

However, I sugeest you should better use the removeAllItems(); method:

combo.removeAllItems();
Milagro answered 3/9, 2012 at 14:1 Comment(3)
+1 because that's the better solution. Though it doesn't explain why OP gets a NullPointerException... ;)Mechanist
The code in the question would normally work. However, it looks like a threading issue. Another thread may be messing with the items.Milagro
Don't explain that to me, explain it to the OP (i.e. include it in your answer ^^).Mechanist
S
3

How about JComboBox.removeAllItems()?

Shishko answered 3/9, 2012 at 14:3 Comment(0)
W
3

You can use

this.combo.removeAllItems();

to remove all the items in JComboBox.

Whin answered 12/4, 2019 at 19:47 Comment(0)
D
2

In second line:

combo.removeItemAt(0);

I think instead of 0 it should be i.

do it in reverse order as:

for(int i=combo.getItemCount()-1;i>=0;i--){
    combo.removeItemAt(i);
}

But in my case combo.removeAllItems() works fine

Droplet answered 22/3, 2015 at 8:51 Comment(0)
E
1

use .removeAllItems() methods to remove all items from combo box.

Essene answered 3/9, 2012 at 14:8 Comment(0)
S
1

The assumption that it is related to another thread is not always true. It can be the thread itself causing the issue.

This exception may happen because an event is triggered when a combo item is removed and in this event handling method you still refer to combobox items.

For example when you delete somewhere (other than in actionPeformed()) in your code the last item from a combo box with combo.removeItemAt(0) or removeAllItems() then still the event actionPerformed will be fired/executed. But very often the actionPerformed() method contains code to react on user actions (user clicked somewhere on the combobox). So, when the last item has been deleted there is no more item in the combobox and any reference to an item or index in actionPerformed() will cause an exception.

The solution to this is to move the code from actionPerformed() to e.g. mouseClicked() or another event handler depending on what you want to do.

Shop answered 11/1, 2013 at 23:59 Comment(0)
P
0

removeAllItems() it does remove all things but after the add data to the combo box it will not show there ,the nullPointException will shows

Peripteral answered 21/10, 2013 at 6:7 Comment(0)
G
0

Use this to remove all the elements from the combo box :

DefaultComboBoxModel model = (DefaultComboBoxModel) ComboBox.getModel();
model.removeAllElements();
Giselagiselbert answered 18/2, 2014 at 13:21 Comment(0)
D
0

Usually it happens because you have an event associated JComboBox. It is solved if you have control item in the JComboBox to act, for example:

jComboBoxExample.addActionListener (new ActionListener () {
   public void actionPerformed (ActionEvent e) {
     do_run ();
   }
});



public void do_run() {
  int n=jComboBoxPerfilDocumentos.getItemCount(); <--THIS IS THE SOLUTION
  if (n> 0) { 
    String x = jComboBoxPerfilDocumentos.getSelectedItem (). ToString ();
  }
}
Douglasdouglashome answered 29/5, 2015 at 8:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.