1

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]);        
}
Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
PadlockCode
  • 37
  • 1
  • 1
  • 5
  • 1
    Create the array itself first: `JComboBox petList[] = new JComboBox[sizeHere];` – rgettman Aug 02 '13 at 23:30
  • unrelated: don't do any manual sizing/locating of components, **ever** - that's the exclusive task of the LayoutManager. – kleopatra Aug 13 '13 at 05:50
  • 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" – Rudi Kershaw Mar 08 '14 at 10:28

2 Answers2

3

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.

Azad
  • 5,047
  • 20
  • 38
  • 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. – PadlockCode Aug 02 '13 at 23:52
  • 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?**](http://stackoverflow.com/questions/4279435/java-how-would-i-dynamically-add-swing-component-to-gui-on-click) – Azad Aug 03 '13 at 00:04
  • And welome to stack, if you have a time, try to read [about **StackOverFlow**](http://stackoverflow.com/about), 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 :) – Azad Aug 03 '13 at 00:06
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]);
}
nanofarad
  • 40,330
  • 4
  • 86
  • 117