Patricia Shanahan wrote:
> Ravi wrote:
>>> How about by initializing it?
>>> JRadioButton jrbtnSub[] = new JRadioButton[3];
>> Thanks, Patricia, I must confess initially I thought that you've given
>> me a wild guess but when I gave it a try it worked!!
>> Can you please explain it too.
> The error was non-initialization of the array reference. I looked at its
> declaration, and the compiler was right, it was not initialized.
> Because it is an array reference, it has to be either null or a pointer
> to an array of JRadioButton or some subclass of JRadioButton.
> I decided to pick an initializer that would let your loop run.
> Specifically, I decided to avoid any exceptions on "jrbtnSub[i] = new
> JRadioButton(temp[i]);" for i in the range 0 through 2.
> That implied the following:
> 1. jrbtnSub cannot be null.
> 2. jrbtnSub must point to a JRadioButton[], not a subclass array.
> 3. jrbtnSub must point to an array with at least three elements.
> Since it is a local variable, and there are no references to elements
> other than the first three, I decided three elements was enough.
> I copied your style of using the literal 3. In my own code, the whole
> code fragment would have looked something like this:
> // Create JRadioButton and add to jrpnlRadBtn
> String[] buttonNames = {"Compilers","Operating Systems",
> "Computer Networks"};
> JRadioButton[] buttons = new JRadioButton[buttonNames.length];
> for(int i=0;i<buttonNames.length;i++) {
> buttons[i] = new JRadioButton(buttonNames[i]);
> jpnlRadBtn.add(buttons[i]);
> }
> Patricia