Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / May 2007

Tip: Looking for answers? Try searching our database.

How to solve this problem of initialization

Thread view: 
Ravi - 13 May 2007 14:07 GMT
/ 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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.