🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: borland.public.jbuilder.java.swing
Article #12435

Re: Array of JCheckBoxes

#12435
From: Jason Burton
Date: Tue, 29 Dec 1998 00:00
29 lines
1241 bytes
>JCheckBox chkBoxes[];
>String names[] = {"A1","A2",...};
>...
>chkBoxes = new JCheckBox[10];
>for (i=0;i<10;i++) {
>    chkBoxes[i].setText(names[i]);
>    panel.add(chkBoxes[i]);
>}
>
>And this gives me a null pointer exception at chkBoxes[i].setText(...);


You need to call a JCheckBox constructor.  I'm guessing that you believe
that the:
chkBoxes = new JCheckBox[10];
line created 10 JCheckBoxes for you.  This is not the case.  It simply
defined the bounds of your array.  You need to create an instance of each
checkbox.  Example:

chkBoxes = new JCheckBox[10];
for (i=0;i<10;i++) {
    chkBoxes[i] = new JCheckBox();
    chkBoxes[i].setText(names[i]);
    panel.add(chkBoxes[i]);
}


Hope this helps,
Jason Burton
Technical Details
Message-ID: <76be27$pj724@forums.borland.com>
Path: arc1-ca.pugleaf.net!unknown!not-for-mail
References: <3689308F.68014E53@netrisk.com>

Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit
X-Google-Thread: 111d45,67c16edf41741832
X-Google-Attributes: gid111d45,public
From: "Jason Burton" <jasonb@tmscorp.com>
Subject: Re: Array of JCheckBoxes
Date: 1998/12/29
Message-ID: <76be27$pj724@forums.borland.com>
X-Deja-AN: 426874700
References: <3689308F.68014E53@netrisk.com>
X-MimeOLE: Produced By Microsoft MimeOLE V4.72.3155.0
Organization: Another Netscape Collabra Server User
Newsgroups: borland.public.jbuilder.java.swing