Hi everyone,

I'm trying to make a simple application displaying some amount of buttons which has added simmilar but different action. The amount of buttons is not known before compiling because it depends on external data. All the buttons are shown in some container.

How did I get it:

-creating a Panel
-creating an array of buttons
-loading the external data = number of buttons
-creating every button and adding it to Panel using loop

So I have an array of buttons shown in Panel. And it works fine.

Now the actions.

Suppose, that every button has to set its label to "I'm clicked" after clicking.

I can't write a piece of code for every button, because I don't know how many buttons will be required by data and moreover it may be a really great amount.

So I have two potential solutions.

The first one:

   int z=0;
   while (z<amountOfButtons){
   button[z].addActionListener(arrayOfActionListeners[z]);
   z++;}       



   z=0;
   while (z<amount){
   arrayOfActionListeners[z]=new ActionListener(){
   public void actionPerformed(ActionEvent e){
   button[z].setLabel("I'm clicked");                   
    }
    };
    z++;} 

The problem is variable z in actionPerformed. NetBeans says "local variable z is accessed from within inner class.".

And the second where the main class implements ActionListener:

   int z=0;
   while (z<amount){
   button[z].addActionListener(this);
   z++;}


   public void actionPerformed(ActionEvent e) {
   int z=0;
   while (z < amountOfButtons){
   if (e.getSource() == button[z]){
   button[z].setLabel("I'm clicked");
   }
   z++;
   }

It gives me it during compiling:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at application.applicationView.actionPerformed(applicationView.java:825)
at java.awt.Button.processActionEvent(Button.java:392)
at java.awt.Button.processEvent(Button.java:360)
at java.awt.Component.dispatchEventImpl(Component.java:4651)
at java.awt.Component.dispatchEvent(Component.java:4481)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:643)
at java.awt.EventQueue.access$000(EventQueue.java:84)
at java.awt.EventQueue$1.run(EventQueue.java:602)
at java.awt.EventQueue$1.run(EventQueue.java:600)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:98)
at java.awt.EventQueue$2.run(EventQueue.java:616)
at java.awt.EventQueue$2.run(EventQueue.java:614)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.AccessControlContext$1.doIntersectionPrivilege(AccessControlContext.java:87)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:613)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:269)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:184)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:174)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:169)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:161)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:122)

I won't paste all the source because it's very very long, includes a lot of pieces of code generated by NetBeans and generally all other things work ;)
I'm not good at programming so I'm looking for the easiest soution which I will able to use.

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
at application.applicationView.actionPerformed(applicationView.java:825)

There is a variable on line 825 with a null value. Look at that line and find the variable with the null value. Then backtrack in the code to find out why that variable does not have a valid non null value.

line 825 is: if (e.getSource() == button[z]){
As far as I know variable z should equals 0 or more.
The second solution seems to be better because there's no as many actionlisteners as buttons.

What about e and button? are either of them null?

All buttons from an array are created with loop before possibility to click.
But e is something new for me and I don't know exactly. I've just tried: ActionListener e = new ActionListener(); but "ActionListener is abstract...". And when I use this ActionListener to something not connected with button[z], it works.

Maybe I shouldn't deal with programming, but this is the last step to end an application I really need so I'm very close.

Thanks for reply.

If you print the the value of e and the value of button you will see which is null.

make the action command for each button the same "button", so that when a button is clicked, the action command is always "button", and execute the code in 'public void actionPerformed(ActionEvent e)' set the clicked buttons label to "I'm clicked".

NormR1 - I'm doing my best to check it out.

FALL3N - It's not what I need. My it's my mistake. In my example all buttons do the same - label "I'm clicked". But as I said mentioned above - in real buttons do similar but different action.

Ok, the button was null. I repaired it but it's not working properly. Just forget about it and please let me ask again.

I've got a code like this:

public void actionPerformed(ActionEvent e) {

if (e.getSource() == button[0]){
jLabel.setText("0");
}
if (e.getSource() == button[1]){
jLabel.setText("1"
}
if (e.getSource() == button[2]){
jLabel.setText("2"
}

But there are about 500 buttons. And the number of buttons is not static.
How can I make it shorter?

Or look at my solution number one. There's a problem there with getting a value of variable from another class. Is there any method to do it? I can't handle with it...

Thanks for help!

How can I make it shorter?

Use a loop.

Better yet is to get the information you need for the setText() call from the button that is the source of the event. Use the reference returned by gerSource(). It is possible to store information with JComponent objects by using its clientProperty. See the API doc for the methods to use.

Thanks a lot NormR1!
It works.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.