Initializing a JComboBox[] array

12,067

Solution 1

Three things you must consider with creating arrays:

  1. Declaration: JComboBox [] petList;
  2. Initial the array: petList = new JComboBox[someSize];
  3. Assigning: petList[i] = new JComboBox();

So, take the petList outside the for-loop (maybe defining it as an instance variable will be better):

public class YourClass{
//instance variables 
private JComboBox[] petList; // you just declared an array of petList
private static final int PET_SIZE = 4;// assuming
//Constructor
public YourClass(){
 petList = new JComboBox[PET_SIZE];  // here you initialed it
 for(int i = 0 ; i < petList.length; i++){
  //.......
  petList[i] = new JComboBox(); // here you assigned each index to avoid `NullPointerException`
 //........
 }
}}

NOTE: this is not a compiled code, the will only demonstrates your solving your problem.

Solution 2

You need to loop. This will incur other errors such as bounds overlapping, but this should be the gist:

JComboBox[] petList = new JComboBox[petStrings.length];
for(int i=0; i<petStrings.length; i++){
    petList[i]=new JComboBox(petStrings[i]);
}
Share:
12,067
PadlockCode
Author by

PadlockCode

Updated on June 04, 2022

Comments

  • PadlockCode
    PadlockCode almost 2 years

    Sorry I am a noob at java, but how do i Initialize the variable petList without setting it equal to null?

    for (int x = 0;x<= buttonPressed;x++){
    
        println("adding box");
        String[] petStrings = { "Withdraw", "Deposit", "Blah", };
    
        //Create the combo box, select item at index 4.
        @SuppressWarnings({ "rawtypes", "unchecked" })
        JComboBox petList[] = null;// = new JComboBox(petStrings);
        petList[x] = new JComboBox(petStrings);
        petList[x].setSelectedIndex(1);
        petList[x].setBounds(119, (buttonPressed *20)+15, 261, 23);
    
        contentPane.add(petList[x]);        
    }
    
    • rgettman
      rgettman almost 11 years
      Create the array itself first: JComboBox petList[] = new JComboBox[sizeHere];
    • kleopatra
      kleopatra almost 11 years
      unrelated: don't do any manual sizing/locating of components, ever - that's the exclusive task of the LayoutManager.
    • Rudi Kershaw
      Rudi Kershaw about 10 years
      Just a small point on convention, when declaring an array it is good practice to put the braces next to the declared type rather than after the identifier. So JComboBox petList[]; would be JComboBox[] petList;. It's easier for others to read like that. Donald Knuth quote that's relevant; "let us concentrate rather on explaining to human beings what we want a computer to do"
  • PadlockCode
    PadlockCode almost 11 years
    Lol im so stupid I cant figure out how to post code in the comments. Also.. This worked but I think My initial objective is coded wrongly. I wanted it to create multiple boxes depending on the size of the int (buttonPressed). but it only created one box regardless.
  • Azad
    Azad almost 11 years
    Yes, I see, this is another subject, because it's not mentioned with your question, try reading this line : How would I dynamically add swing component to GUI on click?
  • Azad
    Azad almost 11 years
    And welome to stack, if you have a time, try to read about StackOverFlow, to know how to up-vote, accept answer especially and other rules because this will not be a last time to ask&answer questions, Good Luck :)