473,395 Members | 1,629 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Array of buttons

How to use array of buttons.
JButton[] b1,b2;
b1[0]=new JButton("0");
b1[1]=new JButton("1");
b1[2]=new JButton("2");
b1[3]=new JButton("3");
b1[4]=new JButton("4");
b1[5]=new JButton("5");
b1[6]=new JButton("6");
b1[7]=new JButton("7");
b1[8]=new JButton("8");
b1[9]=new JButton("9");
for(int i=0;i<=9;i++)
{
p3.add(b1[i]);
}

is showing NullPointerException.
Plz Help
Aug 24 '07 #1
2 2664
r035198x
13,262 8TB
How to use array of buttons.
JButton[] b1,b2;
b1[0]=new JButton("0");
b1[1]=new JButton("1");
b1[2]=new JButton("2");
b1[3]=new JButton("3");
b1[4]=new JButton("4");
b1[5]=new JButton("5");
b1[6]=new JButton("6");
b1[7]=new JButton("7");
b1[8]=new JButton("8");
b1[9]=new JButton("9");
for(int i=0;i<=9;i++)
{
p3.add(b1[i]);
}

is showing NullPointerException.
Plz Help
1.) When posting code please use code tags
2.) After
Expand|Select|Wrap|Line Numbers
  1. Button[] b;
b is still null. To instantiate it you need to do
Expand|Select|Wrap|Line Numbers
  1. Button[] b = new Button[theSize];
3.) You can use a for loop to shorten long lines of code like

Expand|Select|Wrap|Line Numbers
  1.  b1[0]=new JButton("0");
  2.   b1[1]=new JButton("1");
  3.   b1[2]=new JButton("2");
  4.   b1[3]=new JButton("3");
  5.   b1[4]=new JButton("4");
  6.   b1[5]=new JButton("5");
  7.   b1[6]=new JButton("6");
  8.   b1[7]=new JButton("7");
  9.   b1[8]=new JButton("8");
  10.   b1[9]=new JButton("9"); 
to

Expand|Select|Wrap|Line Numbers
  1. for(int i = 1; i < 10; i++) {
  2.              b1[i] = new JButton(""+i);
  3.  } 
Aug 24 '07 #2
nickyeng
254 100+
1.) When posting code please use code tags
2.) After
Expand|Select|Wrap|Line Numbers
  1. Button[] b;
b is still null. To instantiate it you need to do
Expand|Select|Wrap|Line Numbers
  1. Button[] b = new Button[theSize];
3.) You can use a for loop to shorten long lines of code like

Expand|Select|Wrap|Line Numbers
  1.  b1[0]=new JButton("0");
  2.   b1[1]=new JButton("1");
  3.   b1[2]=new JButton("2");
  4.   b1[3]=new JButton("3");
  5.   b1[4]=new JButton("4");
  6.   b1[5]=new JButton("5");
  7.   b1[6]=new JButton("6");
  8.   b1[7]=new JButton("7");
  9.   b1[8]=new JButton("8");
  10.   b1[9]=new JButton("9"); 
to

Expand|Select|Wrap|Line Numbers
  1. for(int i = 1; i < 10; i++) {
  2.              b1[i] = new JButton(""+i);
  3.  } 
gotcha.
I also recommend for loop to instantiate each jbutton object.

good luck, TS.
Aug 24 '07 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Seeker | last post by:
Newbie question here... I have a form with some radio buttons. To verify that at least one of the buttons was chosen I use the following code ("f" is my form object) : var btnChosen; for...
6
by: juli | last post by:
Hello, I need a control which will contain radio buttons that will be added in a loop. I am using this control a source of some other control. I tried to use group box windows control and to add...
10
by: Tor Inge Rislaa | last post by:
Creating Control Array How to create an array of buttons, with common procedures based on the index of the control. How would this Example from VB 6.0 be in VB.NET? Private Sub...
4
by: Tombatore | last post by:
hello people, I've created an array of buttons by code to have an array : buttons(size, size) I am programming a version of mine sweeper, and one button is a clickable rectangle with an image...
2
by: Carl Gilbert | last post by:
Hi I am developing a custom on screen keyboard. So far I have an array of buttons and then using SendKeys to send the text of the button to the active control to receive the text. The only...
1
by: kenny8787 | last post by:
Hi, can anyone help here? I have the following code generated from a database, I want to have javascript calculate the costs of the selected items using radio buttons, subtotal the costs and...
5
by: Peted | last post by:
I know you can iterate through a collection of radio buttons in a panel, using a "for each in control" type iteration that c# supports, but is it possible to iterate through the radio buttons...
6
by: sgottenyc | last post by:
Hello, If you could assist me with the following situation, I would be very grateful. I have a table of data retrieved from database displayed on screen. To each row of data, I have added...
3
by: captainphoenix | last post by:
vb 2005 what i have is an interface designed to act like an order form at a bookstore, like the one a clerk would use to ring up a customer for books. The btnList button connects all the textboxes...
2
by: Chuck | last post by:
How can I make an array of command buttons (or toggle buttons) on a form and have a text box on the same form display the index of the button clicked? This works very well in VB6, but I can't...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.