Hi,
I'm trying to write an application in NetBeans. I have a one Button called Ok which makes an array of buttons and all of them are connected to one actionListener. Here's the code:

    public void actionPerformed(ActionEvent e) {
    if (e.getSource() == Ok){


    int a=500;
    thread=new Thread[a];
    Button button[]=new Button[a];
    int z=0;
    while (z<a){

        button[z] = new Button();
        button[z].setVisible(true);
        button[z].setLabel("Generaed button");

        Panel.add(button[z]);

        z++;

    }

    z=0;
  while (z<a){
  cmp.putClientProperty(button[z], z);
  button[z].addActionListener(this);
    z++;}  

}

 if (e.getSource() != Ok){
 value=cmp.getClientProperty(e.getSource());
 int c = Integer.parseInt(value.toString());
 thread[c] = new Thread() {
 public void run()  {
    button[c].setLabel("test");   // <----This line is a problem.

 }
 }; 
 thread[c].start();
 }
 }

The marked in code line causes Exception in thread "Thread-1" java.lang.NullPointerException.
I guess it's because that button is from another place in the code. I can put here a piece of code like:

        Button button[]=new Button[amount];
        button[c] = new Button();

and it doesn't return any exceptions no more but obviously it's not a solution.
How to get an access to button from code put in public void run()..
I will appreciate every advice.
Thanks

Recommended Answers

All 12 Replies

Where is the button array used on line 34 given values for its elements?

Think about the scope of variable definitions and if you have more than one variable defined with the same name.
If you use unique names for all your variabes (except for loop variables like i and j) you won't have this problem.

Thanks for reply!
The array is about line 7 and defined on the beginning of code in correct place.
Button Ok makes a lot of things used by other generated buttons. Everything is public. I still don't know what to do. The rest of mentioned code is working well when 34 line is replaced by something other.

What is the value of c when you execute line 34? Does the array have a value at that index?

Your code is difficult to read and understand because it is not properly formatted. You should edit the code and properly indent the lines that are at nested logic levels.

Please forgive me the look of code - it wasn't intended.

public void actionPerformed(ActionEvent e)
{
if (e.getSource() == Ok)
{
    int a=500;
    thread=new Thread[a];
    Button button[]=new Button[a];
    int z=0;
    while (z<a)
    {
        button[z] = new Button();
        button[z].setVisible(true);
        button[z].setLabel("Generaed button");
        Panel.add(button[z]);
        z++;
    }
    z=0;
    while (z<a)
    {
        cmp.putClientProperty(button[z], z);
        button[z].addActionListener(this);
        z++;
    }
}
if (e.getSource() != Ok)
{
    value=cmp.getClientProperty(e.getSource());
    int c = Integer.parseInt(value.toString());
    thread[c] = new Thread()
    {
        public void run()
        {
            button[c].setLabel("test");// <----This line is a problem.
        }
    }
    ;
    thread[c].start();
}
}

The value of c depends on the pressed button, it's from 0 to a(=the number of buttons=lenght of array).

value=cmp.getClientProperty(e.getSource());
int c = Integer.parseInt(value.toString());

So. If you press button7, c=7 etc...

But I'm sure it's not the matter of such details. If I define something on the beginning of main class, it's not accessable in public void run(). I can of course define it again inside it, but all lose its value, so there's no sense. And one thing else - please, treat me as idiot. I'm not good at programming.

At line 32 print out the value of c and the value of button[c] to see what the computer is seeing.

Ok, so when I want to do something with for example button[7], it returnes NullPointerException.
When I want to do something with button[c] or just c - it's uncompilable. NetBeans says: "local variable c is accessed from within inner class; needs to be declared final".
I hope it's easy to solve. Thanks for your interesting.

That is confusing. If the code with line 33 compiles then you should be able to write code to print out the values of c and button[c] also.

In fact, I'm not able to compile code with variable c somewhere in public void run().
I can only compile when I set the number of button in code, like button[7] but
apparently it's null, because button[] same like variable c is out of public void run().
Probably the reason is very silly, as much that you can't even suppose thah I ask about it.

You should have said the code did NOT compile. Your first post said you were getting a NullPointerException.

Why are you creating and starting a Thread in lines 29-37?
What happens if you take out the thread code and leave line 33?

  1. Because there will be some piece of code there that will count a time.
  2. Then there is no problem with access. But the application can't work correctly without thread.

I'm sorry if I wasn't precise enough.

Can you make a small complete program that compiles and executes and shows the problem. The small bit of code you have posted does not show the problem. For example why do you need a thread? What is the time counting about?

The entire code is very long, because NetBeans has written two times more than me. But it's still very useful to make GUI. Time counting - according to idea of application - the label of button will be not static. It will change every second. There is more than one button - I must use Thread because of possibility to freeze the application. I will try to make small program only with essential things. Or maybe I will solve it somehow. Thanks for checking the code.

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.