What is the proper way to feed data to JComboBox
? I am trying to feed a String
array to the JComboBox
that was initiated before, and I am getting a NullPointerException
.
Code:
public void readPlayers(){
String[] arr = new String[currentGames.get(currentGame).currentPlayers()];
for(int i = 0; i <currentGames.get(currentGame).currentPlayers(); i++){
arr[i] = "Player " + (i + 1) + currentGames.get(currentGame).getPlayer(i).getId();
}
DefaultComboBoxModel model = new DefaultComboBoxModel(arr);
playersBox.setModel( model);
}
Error:
Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
Edit: My problem here is that I need to update the data in the JComboBox
every time I want to use it because the strings in the array might be different than when I used the combo box the first time.