/ Create JradioButton and add to jrpnlRadBtn
String temp[] = {"Compilers","Operating
Systems","Computer Networks"};
JRadioButton jrbtnSub[];
for(int i=0;i<3;i++) {
jrbtnSub[i] = new JRadioButton(temp[i]);
jpnlRadBtn.add(jrbtnSub[i]);
}
Above code snippet gives the error: JRadioButtonDemo.java:22: variable
jrbtnSub might not have been initialized
jrbtnSub[i] = new JRadioButton(temp[i]);
Eric Sosman - 13 May 2007 14:31 GMT
> / Create JradioButton and add to jrpnlRadBtn
> String temp[] = {"Compilers","Operating
> Systems","Computer Networks"};
> JRadioButton jrbtnSub[];
This says: "jrbtnSub is a variable that can refer to
arrays of JRadioButton references." It does not actually
create an array, it just says that jrbtnSub can refer to
one if you ever decide to create one. For example, you
might continue with
jrbtnSub = new JRadioButton[3]; // or perhaps more
to create a three-element array and set jrbtnSub to point
at it. Or you could declare and initialize in one line with
JRadioButton[] jrbtnSub = new JRadioButton[3];
(Stylistic note: Most Java practitioners prefer
`SomeType[] arrayRef' over `SomeType arrayRef[]', although
both forms are legal. It is felt that the first form collects
all the type information in one place, while the second spreads
it out in separate chunks. Using the second form also calls
into question one's dedication to Java by showing that one has
not yet fully renounced C!)

Signature
Eric Sosman
esosman@acm-dot-org.invalid
Adam Maass - 13 May 2007 14:33 GMT
>/ Create JradioButton and add to jrpnlRadBtn
> String temp[] = {"Compilers","Operating
[quoted text clipped - 8 lines]
> jrbtnSub might not have been initialized
> jrbtnSub[i] = new JRadioButton(temp[i]);
Yup. The more conventional form of the loop might have given you a clue:
for(int i = 0; i < jrbtnSub.length; i++) {
You need to intialize jrbtnSub. You probably mean something like:
JRadioButton jrbtnSub[] = new JRadioButton[temp.length];
-- Adam Maass
Tom Hawtin - 13 May 2007 16:11 GMT
> / Create JradioButton and add to jrpnlRadBtn
> String temp[] = {"Compilers","Operating
[quoted text clipped - 8 lines]
> jrbtnSub might not have been initialized
> jrbtnSub[i] = new JRadioButton(temp[i]);
In addition to what other replies have said, you might consider using a
(java.util.)List instead of an array for the buttons. With a List you
don't need to know the number of elements when you create it (and there
are many other advantages). So your code could become:
String[] subjectNames = {
"Compilers", "Operating Systems",
"Computer Networks"
};
List<JRadioButton> subjectButtons =
new java.util.ArrayList<JRadioButton>();
for (String name : subjectNames) {
JRadioButton button = new JRadioButton(name);
subjectButtons.add(button);
subjectPanel.add(button);
}
(I've changed then identifier names, because I find the likes of "jrbtn"
unreadable and unhelpful.)
Tom Hawtin
Lew - 13 May 2007 19:36 GMT
Ravi wrote:
>> / Create JradioButton and add to jrpnlRadBtn
Please do not multi-post. No one is able to see who else answered your
question or what they said, which is rather rude to the people you want to
help you. It is also rude to the people trying to follow the thread for their
own edification. So please do not multi-post.
Patricia Shanahan gave a good answer over in clj.help, for those of you who
missed it over in cjl.programmer:
Patricia Shanahan wrote:
>>> How about by initializing it?
>>>
[quoted text clipped - 38 lines]
>
> Patricia